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:
782 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
788 mono_type_to_stind (MonoType *type)
799 case MONO_TYPE_BOOLEAN:
811 case MONO_TYPE_FNPTR:
813 case MONO_TYPE_CLASS:
814 case MONO_TYPE_STRING:
815 case MONO_TYPE_OBJECT:
816 case MONO_TYPE_SZARRAY:
817 case MONO_TYPE_ARRAY:
818 return CEE_STIND_REF;
826 case MONO_TYPE_VALUETYPE:
827 if (type->data.klass->enumtype) {
828 t = type->data.klass->enum_basetype->type;
832 case MONO_TYPE_TYPEDBYREF:
835 g_error ("unknown type 0x%02x in type_to_stind", type->type);
841 * Returns the type used in the eval stack when @type is loaded.
842 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
845 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
849 inst->type = STACK_MP;
857 case MONO_TYPE_BOOLEAN:
863 inst->type = STACK_I4;
868 case MONO_TYPE_FNPTR:
869 inst->type = STACK_PTR;
871 case MONO_TYPE_CLASS:
872 case MONO_TYPE_STRING:
873 case MONO_TYPE_OBJECT:
874 case MONO_TYPE_SZARRAY:
875 case MONO_TYPE_ARRAY:
876 inst->type = STACK_OBJ;
880 inst->type = STACK_I8;
884 inst->type = STACK_R8;
886 case MONO_TYPE_VALUETYPE:
887 if (type->data.klass->enumtype) {
888 t = type->data.klass->enum_basetype->type;
891 inst->klass = type->data.klass;
892 inst->type = STACK_VTYPE;
895 case MONO_TYPE_TYPEDBYREF:
896 inst->klass = mono_defaults.typed_reference_class;
897 inst->type = STACK_VTYPE;
900 g_error ("unknown type 0x%02x in eval stack type", type->type);
905 * The following tables are used to quickly validate the IL code in type_from_op ().
908 bin_num_table [STACK_MAX] [STACK_MAX] = {
909 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
910 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
911 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
912 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
913 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
914 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
915 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
916 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
921 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
924 /* reduce the size of this table */
926 bin_int_table [STACK_MAX] [STACK_MAX] = {
927 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
928 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
929 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
930 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, 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},
933 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
934 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
938 bin_comp_table [STACK_MAX] [STACK_MAX] = {
940 {0, 1, 0, 1, 0, 0, 4, 0},
941 {0, 0, 1, 0, 0, 0, 0, 0},
942 {0, 1, 0, 1, 0, 2, 4, 0},
943 {0, 0, 0, 0, 1, 0, 0, 0},
944 {0, 0, 0, 2, 0, 1, 0, 0},
945 {0, 4, 0, 4, 0, 0, 3, 0},
946 {0, 0, 0, 0, 0, 0, 0, 0},
949 /* reduce the size of this table */
951 shift_table [STACK_MAX] [STACK_MAX] = {
952 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
953 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
954 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
955 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
956 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
957 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
958 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
959 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
963 * Tables to map from the non-specific opcode to the matching
964 * type-specific opcode.
966 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
968 binops_op_map [STACK_MAX] = {
969 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
972 /* handles from CEE_NEG to CEE_CONV_U8 */
974 unops_op_map [STACK_MAX] = {
975 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
978 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
980 ovfops_op_map [STACK_MAX] = {
981 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
984 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
986 ovf2ops_op_map [STACK_MAX] = {
987 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
990 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
992 ovf3ops_op_map [STACK_MAX] = {
993 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
996 /* handles from CEE_CEQ to CEE_CLT_UN */
998 ceqops_op_map [STACK_MAX] = {
999 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1003 * Sets ins->type (the type on the eval stack) according to the
1004 * type of the opcode and the arguments to it.
1005 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1007 * FIXME: this function sets ins->type unconditionally in some cases, but
1008 * it should set it to invalid for some types (a conv.x on an object)
1011 type_from_op (MonoInst *ins) {
1012 switch (ins->opcode) {
1019 /* FIXME: check unverifiable args for STACK_MP */
1020 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1021 ins->opcode += binops_op_map [ins->type];
1028 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1029 ins->opcode += binops_op_map [ins->type];
1034 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1035 ins->opcode += binops_op_map [ins->type];
1038 /* FIXME: handle some specifics with ins->next->type */
1039 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1043 case 256+CEE_CGT_UN:
1045 case 256+CEE_CLT_UN:
1046 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1047 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1051 ins->type = neg_table [ins->inst_i0->type];
1052 ins->opcode += unops_op_map [ins->type];
1055 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1056 ins->type = ins->inst_i0->type;
1058 ins->type = STACK_INV;
1059 ins->opcode += unops_op_map [ins->type];
1065 ins->type = STACK_I4;
1066 ins->opcode += unops_op_map [ins->inst_i0->type];
1069 ins->type = STACK_R8;
1070 switch (ins->inst_i0->type) {
1075 ins->opcode = OP_LCONV_TO_R_UN;
1079 case CEE_CONV_OVF_I1:
1080 case CEE_CONV_OVF_U1:
1081 case CEE_CONV_OVF_I2:
1082 case CEE_CONV_OVF_U2:
1083 case CEE_CONV_OVF_I4:
1084 case CEE_CONV_OVF_U4:
1085 ins->type = STACK_I4;
1086 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1088 case CEE_CONV_OVF_I_UN:
1089 case CEE_CONV_OVF_U_UN:
1090 ins->type = STACK_PTR;
1091 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1093 case CEE_CONV_OVF_I1_UN:
1094 case CEE_CONV_OVF_I2_UN:
1095 case CEE_CONV_OVF_I4_UN:
1096 case CEE_CONV_OVF_U1_UN:
1097 case CEE_CONV_OVF_U2_UN:
1098 case CEE_CONV_OVF_U4_UN:
1099 ins->type = STACK_I4;
1100 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1103 ins->type = STACK_PTR;
1104 switch (ins->inst_i0->type) {
1110 ins->opcode = OP_LCONV_TO_U;
1113 ins->opcode = OP_FCONV_TO_U;
1119 ins->type = STACK_I8;
1120 ins->opcode += unops_op_map [ins->inst_i0->type];
1122 case CEE_CONV_OVF_I8:
1123 case CEE_CONV_OVF_U8:
1124 ins->type = STACK_I8;
1125 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1127 case CEE_CONV_OVF_U8_UN:
1128 case CEE_CONV_OVF_I8_UN:
1129 ins->type = STACK_I8;
1130 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1134 ins->type = STACK_R8;
1135 ins->opcode += unops_op_map [ins->inst_i0->type];
1138 ins->type = STACK_R8;
1142 ins->type = STACK_I4;
1143 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1146 case CEE_CONV_OVF_I:
1147 case CEE_CONV_OVF_U:
1148 ins->type = STACK_PTR;
1149 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1152 case CEE_ADD_OVF_UN:
1154 case CEE_MUL_OVF_UN:
1156 case CEE_SUB_OVF_UN:
1157 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1158 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1161 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1168 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1171 /* map ldelem.x to the matching ldind.x opcode */
1173 ldelem_to_ldind [] = {
1187 /* map stelem.x to the matching stind.x opcode */
1189 stelem_to_stind [] = {
1203 param_table [STACK_MAX] [STACK_MAX] = {
1208 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1212 switch (args->type) {
1222 for (i = 0; i < sig->param_count; ++i) {
1223 switch (args [i].type) {
1227 if (!sig->params [i]->byref)
1231 if (sig->params [i]->byref)
1233 switch (sig->params [i]->type) {
1234 case MONO_TYPE_CLASS:
1235 case MONO_TYPE_STRING:
1236 case MONO_TYPE_OBJECT:
1237 case MONO_TYPE_SZARRAY:
1238 case MONO_TYPE_ARRAY:
1245 if (sig->params [i]->byref)
1247 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1256 /*if (!param_table [args [i].type] [sig->params [i]->type])
1264 * When we need a pointer to the current domain many times in a method, we
1265 * call mono_domain_get() once and we store the result in a local variable.
1266 * This function returns the variable that represents the MonoDomain*.
1268 inline static MonoInst *
1269 mono_get_domainvar (MonoCompile *cfg)
1271 if (!cfg->domainvar)
1272 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1273 return cfg->domainvar;
1277 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1280 int num = cfg->num_varinfo;
1282 if ((num + 1) >= cfg->varinfo_count) {
1283 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1284 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1285 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1288 mono_jit_stats.allocate_var++;
1290 MONO_INST_NEW (cfg, inst, opcode);
1291 inst->inst_c0 = num;
1292 inst->inst_vtype = type;
1293 inst->klass = mono_class_from_mono_type (type);
1294 /* if set to 1 the variable is native */
1297 cfg->varinfo [num] = inst;
1299 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1300 MONO_INIT_VARINFO (cfg->vars [num], num);
1303 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1308 type_from_stack_type (MonoInst *ins) {
1309 switch (ins->type) {
1310 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1311 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1312 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1313 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1314 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1315 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1316 case STACK_VTYPE: return &ins->klass->byval_arg;
1318 g_error ("stack type %d to montype not handled\n", ins->type);
1324 array_access_to_klass (int opcode)
1328 return mono_defaults.byte_class;
1330 return mono_defaults.uint16_class;
1333 return mono_defaults.int_class;
1336 return mono_defaults.sbyte_class;
1339 return mono_defaults.int16_class;
1342 return mono_defaults.int32_class;
1344 return mono_defaults.uint32_class;
1347 return mono_defaults.int64_class;
1350 return mono_defaults.single_class;
1353 return mono_defaults.double_class;
1354 case CEE_LDELEM_REF:
1355 case CEE_STELEM_REF:
1356 return mono_defaults.object_class;
1358 g_assert_not_reached ();
1364 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1368 MONO_ADD_INS (bb, inst);
1371 switch (bb->last_ins->opcode) {
1384 while (prev->next && prev->next != bb->last_ins)
1386 if (prev == bb->code) {
1387 if (bb->last_ins == bb->code) {
1388 inst->next = bb->code;
1391 inst->next = prev->next;
1395 inst->next = bb->last_ins;
1399 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1401 MONO_ADD_INS (bb, inst);
1407 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1409 MonoInst *inst, *load;
1411 NEW_TEMPLOAD (cfg, load, src);
1413 NEW_TEMPSTORE (cfg, inst, dest, load);
1414 if (inst->opcode == CEE_STOBJ) {
1415 NEW_TEMPLOADA (cfg, inst, dest);
1416 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1418 inst->cil_code = NULL;
1419 mono_add_ins_to_end (bb, inst);
1424 * We try to share variables when possible
1427 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1432 /* inlining can result in deeper stacks */
1433 if (slot >= ((MonoMethodNormal *)cfg->method)->header->max_stack)
1434 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1436 pos = ins->type - 1 + slot * STACK_MAX;
1438 switch (ins->type) {
1445 if ((vnum = cfg->intvars [pos]))
1446 return cfg->varinfo [vnum];
1447 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1448 cfg->intvars [pos] = res->inst_c0;
1451 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1457 * This function is called to handle items that are left on the evaluation stack
1458 * at basic block boundaries. What happens is that we save the values to local variables
1459 * and we reload them later when first entering the target basic block (with the
1460 * handle_loaded_temps () function).
1461 * A single joint point will use the same variables (stored in the array bb->out_stack or
1462 * bb->in_stack, if the basic block is before or after the joint point).
1465 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1467 MonoBasicBlock *outb;
1468 MonoInst *inst, **locals;
1472 if (cfg->verbose_level > 3)
1473 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1474 if (!bb->out_scount) {
1476 bb->out_scount = count;
1477 //g_print ("bblock %d has out:", bb->block_num);
1478 for (i = 0; i < bb->out_count; ++i) {
1479 outb = bb->out_bb [i];
1480 //g_print (" %d", outb->block_num);
1481 if (outb->in_stack) {
1483 bb->out_stack = outb->in_stack;
1489 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1490 for (i = 0; i < count; ++i) {
1492 /* try to reuse temps already allocated for this purpouse, if they occupy the same
1493 * stack slot and if they are of the same type. */
1494 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1496 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1501 locals = bb->out_stack;
1502 for (i = 0; i < count; ++i) {
1503 /* add store ops at the end of the bb, before the branch */
1504 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1505 if (inst->opcode == CEE_STOBJ) {
1506 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1507 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1509 inst->cil_code = sp [i]->cil_code;
1510 mono_add_ins_to_end (bb, inst);
1512 if (cfg->verbose_level > 3)
1513 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1516 for (i = 0; i < bb->out_count; ++i) {
1517 outb = bb->out_bb [i];
1518 if (outb->in_scount)
1519 continue; /* check they are the same locals */
1520 outb->in_scount = count;
1521 outb->in_stack = locals;
1527 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1532 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1536 case MONO_TYPE_VOID:
1537 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1540 case MONO_TYPE_BOOLEAN:
1543 case MONO_TYPE_CHAR:
1546 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1550 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1551 case MONO_TYPE_CLASS:
1552 case MONO_TYPE_STRING:
1553 case MONO_TYPE_OBJECT:
1554 case MONO_TYPE_SZARRAY:
1555 case MONO_TYPE_ARRAY:
1556 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1559 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1562 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1563 case MONO_TYPE_VALUETYPE:
1564 if (type->data.klass->enumtype) {
1565 t = type->data.klass->enum_basetype->type;
1568 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1569 case MONO_TYPE_TYPEDBYREF:
1570 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1572 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1578 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1580 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1582 ji->ip.label = label;
1583 ji->type = MONO_PATCH_INFO_SWITCH;
1584 ji->data.table = bbs;
1585 ji->next = cfg->patch_info;
1586 ji->table_size = num_blocks;
1587 cfg->patch_info = ji;
1591 * When we add a tree of instructions, we need to ensure the instructions currently
1592 * on the stack are executed before (like, if we load a value from a local).
1593 * We ensure this by saving the currently loaded values to temps and rewriting the
1594 * instructions to load the values.
1595 * This is not done for opcodes that terminate a basic block (because it's handled already
1596 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1599 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1601 MonoInst *load, *store, *temp, *ins;
1603 while (stack < sp) {
1605 /* handle also other constants */
1606 if (ins->opcode != OP_ICONST) {
1607 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1608 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1609 store->cil_code = ins->cil_code;
1610 if (store->opcode == CEE_STOBJ) {
1611 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1612 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1614 MONO_ADD_INS (bblock, store);
1615 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1616 load->cil_code = ins->cil_code;
1624 * Prepare arguments for passing to a function call.
1625 * Return a non-zero value if the arguments can't be passed to the given
1627 * The type checks are not yet complete and some conversions may need
1628 * casts on 32 or 64 bit architectures.
1631 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1636 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1640 for (i = 0; i < sig->param_count; ++i) {
1641 if (sig->params [i]->byref) {
1643 * check the result of ldelema is only passed as an argument if the byref
1644 * type matches exactly the array element type.
1645 * FIXME: if the argument as been saved on the stack as part of the
1646 * interface variable code (the value was on the stack at a basic block boundary)
1647 * we need to add the check in that case, too.
1649 if (args [i]->opcode == CEE_LDELEMA) {
1651 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1652 if (!exact_class->valuetype) {
1653 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1654 check->cil_code = args [i]->cil_code;
1655 check->klass = exact_class;
1656 check->inst_left = args [i]->inst_left;
1657 check->type = STACK_OBJ;
1658 args [i]->inst_left = check;
1661 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1665 simple_type = sig->params [i]->type;
1667 switch (simple_type) {
1668 case MONO_TYPE_VOID:
1673 case MONO_TYPE_BOOLEAN:
1676 case MONO_TYPE_CHAR:
1679 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1685 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1688 case MONO_TYPE_CLASS:
1689 case MONO_TYPE_STRING:
1690 case MONO_TYPE_OBJECT:
1691 case MONO_TYPE_SZARRAY:
1692 case MONO_TYPE_ARRAY:
1693 if (args [i]->type != STACK_OBJ)
1698 if (args [i]->type != STACK_I8)
1703 if (args [i]->type != STACK_R8)
1706 case MONO_TYPE_VALUETYPE:
1707 if (sig->params [i]->data.klass->enumtype) {
1708 simple_type = sig->params [i]->data.klass->enum_basetype->type;
1711 if (args [i]->type != STACK_VTYPE)
1714 case MONO_TYPE_TYPEDBYREF:
1715 if (args [i]->type != STACK_VTYPE)
1719 g_error ("unknown type 0x%02x in check_call_signature", simple_type);
1726 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1727 const guint8 *ip, gboolean to_end)
1729 MonoInst *temp, *store, *ins = (MonoInst*)call;
1730 MonoType *ret = sig->ret;
1732 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1734 call->inst.type = STACK_OBJ;
1735 call->inst.opcode = CEE_CALL;
1736 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1738 type_to_eval_stack_type (ret, ins);
1739 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1742 if (MONO_TYPE_ISSTRUCT (ret)) {
1745 /* we use this to allocate native sized structs */
1746 temp->unused = sig->pinvoke;
1748 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1749 if (call->inst.opcode == OP_VCALL)
1750 ins->inst_left = loada;
1752 ins->inst_right = loada; /* a virtual or indirect call */
1755 mono_add_ins_to_end (bblock, ins);
1757 MONO_ADD_INS (bblock, ins);
1759 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1760 store->cil_code = ip;
1762 mono_add_ins_to_end (bblock, store);
1764 MONO_ADD_INS (bblock, store);
1766 return temp->inst_c0;
1769 mono_add_ins_to_end (bblock, ins);
1771 MONO_ADD_INS (bblock, ins);
1776 inline static MonoCallInst *
1777 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1778 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1784 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1786 call->inst.cil_code = ip;
1788 call->signature = sig;
1789 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1791 for (arg = call->out_args; arg;) {
1792 MonoInst *narg = arg->next;
1797 mono_add_ins_to_end (bblock, arg);
1799 MONO_ADD_INS (bblock, arg);
1806 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1807 MonoInst **args, MonoInst *addr, const guint8 *ip)
1809 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1811 call->inst.inst_i0 = addr;
1813 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1816 static MonoCallInst*
1817 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1818 MonoInst **args, const guint8 *ip, MonoInst *this)
1820 gboolean virtual = this != NULL;
1823 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
1825 if (this && sig->hasthis &&
1826 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
1827 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
1828 call->method = mono_marshal_get_remoting_invoke_with_check (method);
1830 call->method = method;
1832 call->inst.flags |= MONO_INST_HAS_METHOD;
1833 call->inst.inst_left = this;
1839 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
1840 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
1842 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
1844 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
1848 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
1849 MonoInst **args, const guint8 *ip, gboolean to_end)
1855 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
1857 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
1861 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
1863 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
1866 g_warning ("unregistered JIT ICall");
1867 g_assert_not_reached ();
1870 return mono_emit_native_call (cfg, bblock, info->wrapper, info->sig, args, ip, FALSE);
1874 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
1876 MonoInst *ins, *temp = NULL, *store, *load;
1877 MonoInst *last_arg = NULL;
1881 /*g_print ("emulating: ");
1882 mono_print_tree (tree);
1884 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
1885 ins = (MonoInst*)call;
1887 call->inst.cil_code = tree->cil_code;
1889 call->signature = info->sig;
1891 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
1893 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1894 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
1895 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1896 store->cil_code = tree->cil_code;
1901 nargs = info->sig->param_count + info->sig->hasthis;
1903 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
1906 last_arg->next = store;
1908 if (cfg->prev_ins) {
1909 store->next = cfg->prev_ins->next;
1911 cfg->prev_ins->next = call->out_args;
1913 cfg->prev_ins->next = store;
1915 store->next = cfg->cbb->code;
1917 cfg->cbb->code = call->out_args;
1919 cfg->cbb->code = store;
1923 call->fptr = info->wrapper;
1925 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1926 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1931 static MonoMethodSignature *
1932 mono_get_element_address_signature (int arity)
1934 static GHashTable *sighash = NULL;
1935 MonoMethodSignature *res;
1939 sighash = g_hash_table_new (NULL, NULL);
1942 if ((res = g_hash_table_lookup (sighash, (gpointer)arity)))
1945 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
1947 res->params [0] = &mono_defaults.array_class->byval_arg;
1949 for (i = 1; i <= arity; i++)
1950 res->params [i] = &mono_defaults.int_class->byval_arg;
1952 res->ret = &mono_defaults.int_class->byval_arg;
1954 g_hash_table_insert (sighash, (gpointer)arity, res);
1960 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
1961 MonoInst *iargs [3];
1967 * This check breaks with spilled vars... need to handle it during verification anyway.
1968 * g_assert (klass && klass == src->klass && klass == dest->klass);
1972 n = mono_class_native_size (klass, &align);
1974 n = mono_class_value_size (klass, &align);
1976 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
1978 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
1979 inst->inst_left = dest;
1980 inst->inst_right = src;
1981 inst->cil_code = ip;
1983 MONO_ADD_INS (bblock, inst);
1988 NEW_ICONST (cfg, iargs [2], n);
1990 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
1994 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
1996 MonoInst *iargs [2];
1997 MonoInst *ins, *zero_int32;
2000 NEW_ICONST (cfg, zero_int32, 0);
2002 mono_class_init (klass);
2003 n = mono_class_value_size (klass, NULL);
2004 MONO_INST_NEW (cfg, ins, 0);
2006 ins->inst_left = dest;
2007 ins->inst_right = zero_int32;
2010 ins->opcode = CEE_STIND_I1;
2011 MONO_ADD_INS (bblock, ins);
2014 ins->opcode = CEE_STIND_I2;
2015 MONO_ADD_INS (bblock, ins);
2018 ins->opcode = CEE_STIND_I4;
2019 MONO_ADD_INS (bblock, ins);
2022 if (n <= sizeof (gpointer) * 5) {
2023 ins->opcode = OP_MEMSET;
2026 MONO_ADD_INS (bblock, ins);
2029 handle_loaded_temps (cfg, bblock, stack_start, sp);
2030 NEW_ICONST (cfg, ins, n);
2033 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2038 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2041 needs_cctor_run (MonoClass *klass, MonoMethod *caller)
2046 for (i = 0; i < klass->method.count; ++i) {
2047 method = klass->methods [i];
2048 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2049 (strcmp (".cctor", method->name) == 0)) {
2050 if (caller == method)
2059 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2061 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
2062 MonoMethodSignature *signature = method->signature;
2066 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2067 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2068 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2069 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2070 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2071 (method->klass->marshalbyref) ||
2072 !header || header->num_clauses ||
2073 /* fixme: why cant we inline valuetype returns? */
2074 MONO_TYPE_ISSTRUCT (signature->ret))
2077 /* its not worth to inline methods with valuetype arguments?? */
2078 for (i = 0; i < signature->param_count; i++) {
2079 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2085 * if we can initialize the class of the method right away, we do,
2086 * otherwise we don't allow inlining if the class needs initialization,
2087 * since it would mean inserting a call to mono_runtime_class_init()
2088 * inside the inlined code
2090 if (!(cfg->opt & MONO_OPT_SHARED)) {
2091 vtable = mono_class_vtable (cfg->domain, method->klass);
2092 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
2093 mono_runtime_class_init (vtable);
2094 else if (!vtable->initialized && needs_cctor_run (method->klass, NULL))
2098 * If we're compiling for shared code
2099 * the cctor will need to be run at aot method load time, for example,
2100 * or at the end of the compilation of the inlining method.
2102 if (needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2105 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2107 /* also consider num_locals? */
2108 if (header->code_size < 20)
2115 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2119 MonoMethodSignature *esig;
2121 rank = cmethod->signature->param_count - (is_set? 1: 0);
2123 * FIXME: handle TypeMismatch for set or use the slow path
2126 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2128 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2129 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2130 addr->inst_left = sp [0];
2131 addr->inst_right = indexes;
2132 addr->cil_code = ip;
2133 addr->type = STACK_MP;
2134 addr->klass = cmethod->klass;
2137 esig = mono_get_element_address_signature (rank);
2138 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2139 NEW_TEMPLOAD (cfg, addr, temp);
2144 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2149 if (cmethod->klass == mono_defaults.string_class) {
2150 if (cmethod->name [0] != 'g' || strcmp (cmethod->name, "get_Chars"))
2153 } else if (cmethod->klass == mono_defaults.math_class) {
2154 if (strcmp (cmethod->name, "Sin") == 0)
2156 else if (strcmp (cmethod->name, "Cos") == 0)
2158 else if (strcmp (cmethod->name, "Tan") == 0)
2160 else if (strcmp (cmethod->name, "Atan") == 0)
2162 else if (strcmp (cmethod->name, "Sqrt") == 0)
2164 else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8)
2167 /* OP_FREM is not IEEE compatible */
2168 else if (strcmp (cmethod->name, "IEEERemainder") == 0)
2173 } else if (cmethod->klass == mono_defaults.array_class) {
2174 if (strcmp (cmethod->name, "get_Rank") == 0)
2176 else if (strcmp (cmethod->name, "get_Length") == 0)
2183 pc = fsig->param_count + fsig->hasthis;
2184 MONO_INST_NEW (cfg, ins, op);
2187 ins->inst_i0 = args [0];
2189 ins->inst_i1 = args [1];
2196 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2198 MonoInst *store, *temp;
2201 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2203 if (!sig->hasthis && sig->param_count == 0)
2207 if (sp [0]->opcode == OP_ICONST) {
2210 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2212 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2213 store->cil_code = sp [0]->cil_code;
2214 MONO_ADD_INS (bblock, store);
2219 for (i = 0; i < sig->param_count; ++i) {
2220 if (sp [0]->opcode == OP_ICONST) {
2223 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2225 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2226 store->cil_code = sp [0]->cil_code;
2227 if (store->opcode == CEE_STOBJ) {
2228 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2229 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2231 MONO_ADD_INS (bblock, store);
2239 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2240 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b)
2242 MonoInst *ins, *rvar = NULL;
2243 MonoMethodHeader *cheader;
2244 MonoBasicBlock *ebblock, *sbblock;
2245 int i, costs, new_locals_offset;
2247 if (cfg->verbose_level > 2)
2248 g_print ("INLINE START %p %s\n", cmethod, mono_method_full_name (cmethod, TRUE));
2250 cheader = ((MonoMethodNormal *)cmethod)->header;
2252 if (!cmethod->inline_info) {
2253 mono_jit_stats.inlineable_methods++;
2254 cmethod->inline_info = 1;
2256 /* allocate space to store the return value */
2257 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2258 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2261 /* allocate local variables */
2262 new_locals_offset = cfg->num_varinfo;
2263 for (i = 0; i < cheader->num_locals; ++i)
2264 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2266 /* allocate starte and end blocks */
2267 sbblock = NEW_BBLOCK (cfg);
2268 sbblock->block_num = cfg->num_bblocks++;
2269 sbblock->real_offset = real_offset;
2271 ebblock = NEW_BBLOCK (cfg);
2272 ebblock->block_num = cfg->num_bblocks++;
2273 ebblock->real_offset = real_offset;
2275 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2277 if (costs >= 0 && costs < 60) {
2278 if (cfg->verbose_level > 2)
2279 g_print ("INLINE END %s\n", mono_method_full_name (cmethod, TRUE));
2281 mono_jit_stats.inlined_methods++;
2283 /* always add some code to avoid block split failures */
2284 MONO_INST_NEW (cfg, ins, CEE_NOP);
2285 MONO_ADD_INS (bblock, ins);
2288 bblock->next_bb = sbblock;
2289 link_bblock (cfg, bblock, sbblock);
2292 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2298 if (cfg->verbose_level > 2)
2299 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2305 * Some of these comments may well be out-of-date.
2306 * Design decisions: we do a single pass over the IL code (and we do bblock
2307 * splitting/merging in the few cases when it's required: a back jump to an IL
2308 * address that was not already seen as bblock starting point).
2309 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2310 * Complex operations are decomposed in simpler ones right away. We need to let the
2311 * arch-specific code peek and poke inside this process somehow (except when the
2312 * optimizations can take advantage of the full semantic info of coarse opcodes).
2313 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2314 * MonoInst->opcode initially is the IL opcode or some simplification of that
2315 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2316 * opcode with value bigger than OP_LAST.
2317 * At this point the IR can be handed over to an interpreter, a dumb code generator
2318 * or to the optimizing code generator that will translate it to SSA form.
2320 * Profiling directed optimizations.
2321 * We may compile by default with few or no optimizations and instrument the code
2322 * or the user may indicate what methods to optimize the most either in a config file
2323 * or through repeated runs where the compiler applies offline the optimizations to
2324 * each method and then decides if it was worth it.
2327 * * consider using an array instead of an hash table (bb_hash)
2330 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2331 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2332 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2334 /* offset from br.s -> br like opcodes */
2335 #define BIG_BRANCH_OFFSET 13
2338 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end)
2340 unsigned char *ip = start;
2341 unsigned char *target;
2344 MonoBasicBlock *bblock;
2345 const MonoOpcode *opcode;
2348 cli_addr = ip - start;
2349 i = mono_opcode_value ((const guint8 **)&ip);
2350 opcode = &mono_opcodes [i];
2351 switch (opcode->argument) {
2352 case MonoInlineNone:
2355 case MonoInlineString:
2356 case MonoInlineType:
2357 case MonoInlineField:
2358 case MonoInlineMethod:
2361 case MonoShortInlineR:
2368 case MonoShortInlineVar:
2369 case MonoShortInlineI:
2372 case MonoShortInlineBrTarget:
2373 target = start + cli_addr + 2 + (signed char)ip [1];
2374 GET_BBLOCK (cfg, bbhash, bblock, target);
2377 case MonoInlineBrTarget:
2378 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2379 GET_BBLOCK (cfg, bbhash, bblock, target);
2382 case MonoInlineSwitch: {
2383 guint32 n = read32 (ip + 1);
2386 cli_addr += 5 + 4 * n;
2387 target = start + cli_addr;
2388 GET_BBLOCK (cfg, bbhash, bblock, target);
2390 for (j = 0; j < n; ++j) {
2391 target = start + cli_addr + (gint32)read32 (ip);
2392 GET_BBLOCK (cfg, bbhash, bblock, target);
2402 g_assert_not_reached ();
2411 * mono_method_to_ir: translates IL into basic blocks containing trees
2414 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2415 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2416 guint inline_offset, gboolean is_virtual_call)
2418 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2419 MonoInst *ins, **sp, **stack_start;
2420 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2422 MonoMethod *cmethod;
2423 MonoInst **arg_array;
2424 MonoMethodHeader *header;
2426 guint32 token, ins_flag;
2428 unsigned char *ip, *end, *target;
2429 static double r8_0 = 0.0;
2430 MonoMethodSignature *sig;
2431 MonoType **param_types;
2432 GList *bb_recheck = NULL, *tmp;
2433 int i, n, start_new_bblock, align;
2434 int num_calls = 0, inline_costs = 0;
2435 int *filter_lengths = NULL;
2436 int breakpoint_id = 0;
2439 image = method->klass->image;
2440 header = ((MonoMethodNormal *)method)->header;
2441 sig = method->signature;
2442 ip = (unsigned char*)header->code;
2443 end = ip + header->code_size;
2444 mono_jit_stats.cil_code_size += header->code_size;
2446 if (cfg->method == method) {
2448 bbhash = cfg->bb_hash;
2450 real_offset = inline_offset;
2451 bbhash = g_hash_table_new (g_direct_hash, NULL);
2454 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
2455 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
2457 dont_inline = g_list_prepend (dont_inline, method);
2458 if (cfg->method == method) {
2461 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2462 start_bblock->cil_code = NULL;
2463 start_bblock->cil_length = 0;
2464 start_bblock->block_num = cfg->num_bblocks++;
2467 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2468 end_bblock->cil_code = NULL;
2469 end_bblock->cil_length = 0;
2470 end_bblock->block_num = cfg->num_bblocks++;
2471 g_assert (cfg->num_bblocks == 2);
2473 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2474 for (i = sig->hasthis + sig->param_count - 1; i >= 0; i--)
2475 arg_array [i] = cfg->varinfo [i];
2477 if (mono_compile_aot)
2478 cfg->opt |= MONO_OPT_SHARED;
2480 if (header->num_clauses) {
2481 int size = sizeof (int) * header->num_clauses;
2482 filter_lengths = alloca (size);
2483 memset (filter_lengths, 0, size);
2485 cfg->spvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2486 /* prevent it from being register allocated */
2487 cfg->spvar->flags |= MONO_INST_INDIRECT;
2489 /* handle exception clauses */
2490 for (i = 0; i < header->num_clauses; ++i) {
2491 //unsigned char *p = ip;
2492 MonoExceptionClause *clause = &header->clauses [i];
2493 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2494 tblock->real_offset = clause->try_offset;
2495 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2496 tblock->real_offset = clause->handler_offset;
2498 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2499 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2500 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2501 MONO_ADD_INS (tblock, ins);
2504 /*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);
2506 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2508 /* catch and filter blocks get the exception object on the stack */
2509 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2510 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2511 /* mostly like handle_stack_args (), but just sets the input args */
2512 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2514 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2515 /* prevent it from being register allocated */
2516 cfg->exvar->flags |= MONO_INST_INDIRECT;
2518 tblock->in_scount = 1;
2519 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2520 tblock->in_stack [0] = cfg->exvar;
2522 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2523 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2524 tblock->real_offset = clause->token_or_filter;
2525 tblock->in_scount = 1;
2526 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2527 tblock->in_stack [0] = cfg->exvar;
2528 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2529 MONO_ADD_INS (tblock, ins);
2535 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2536 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2539 /* FIRST CODE BLOCK */
2540 bblock = NEW_BBLOCK (cfg);
2541 bblock->cil_code = ip;
2543 ADD_BBLOCK (cfg, bbhash, bblock);
2545 if (cfg->method == method) {
2546 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2547 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2548 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2549 MONO_ADD_INS (bblock, ins);
2553 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED)))) {
2554 /* we use a separate basic block for the initialization code */
2555 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2556 init_localsbb->real_offset = real_offset;
2557 start_bblock->next_bb = init_localsbb;
2558 init_localsbb->next_bb = bblock;
2559 link_bblock (cfg, start_bblock, init_localsbb);
2560 link_bblock (cfg, init_localsbb, bblock);
2561 init_localsbb->block_num = cfg->num_bblocks++;
2563 start_bblock->next_bb = bblock;
2564 link_bblock (cfg, start_bblock, bblock);
2567 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end))
2570 mono_debug_init_method (cfg, bblock, breakpoint_id);
2572 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * (sig->hasthis + sig->param_count));
2574 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2575 for (n = 0; n < sig->param_count; ++n)
2576 param_types [n + sig->hasthis] = sig->params [n];
2578 /* do this somewhere outside - not here */
2579 NEW_ICONST (cfg, zero_int32, 0);
2580 NEW_ICONST (cfg, zero_int64, 0);
2581 zero_int64->type = STACK_I8;
2582 NEW_PCONST (cfg, zero_ptr, 0);
2583 NEW_PCONST (cfg, zero_obj, 0);
2584 zero_obj->type = STACK_OBJ;
2586 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2587 zero_r8->type = STACK_R8;
2588 zero_r8->inst_p0 = &r8_0;
2590 /* add a check for this != NULL to inlined methods */
2591 if (is_virtual_call) {
2592 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2593 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2595 MONO_ADD_INS (bblock, ins);
2598 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2599 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2602 start_new_bblock = 0;
2605 if (cfg->method == method)
2606 real_offset = ip - header->code;
2608 real_offset = inline_offset;
2610 if (start_new_bblock) {
2611 bblock->cil_length = ip - bblock->cil_code;
2612 if (start_new_bblock == 2) {
2613 g_assert (ip == tblock->cil_code);
2615 GET_BBLOCK (cfg, bbhash, tblock, ip);
2617 bblock->next_bb = tblock;
2619 start_new_bblock = 0;
2620 for (i = 0; i < bblock->in_scount; ++i) {
2621 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2625 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2626 link_bblock (cfg, bblock, tblock);
2627 if (sp != stack_start) {
2628 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2631 bblock->next_bb = tblock;
2633 for (i = 0; i < bblock->in_scount; ++i) {
2634 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2640 if (cfg->coverage_info) {
2641 MonoInst *store, *one;
2642 guint32 cil_offset = ip - header->code;
2643 cfg->coverage_info->data [cil_offset].cil_code = ip;
2645 /* TODO: Use an increment here */
2646 NEW_ICONST (cfg, one, 1);
2649 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
2652 MONO_INST_NEW (cfg, store, CEE_STIND_I);
2653 store->cil_code = ip;
2654 store->inst_left = ins;
2655 store->inst_right = one;
2657 MONO_ADD_INS (bblock, store);
2660 if (cfg->verbose_level > 3)
2661 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2668 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2669 ins->cil_code = ip++;
2670 MONO_ADD_INS (bblock, ins);
2676 CHECK_STACK_OVF (1);
2677 n = (*ip)-CEE_LDARG_0;
2678 NEW_ARGLOAD (cfg, ins, n);
2679 ins->cil_code = ip++;
2686 CHECK_STACK_OVF (1);
2687 n = (*ip)-CEE_LDLOC_0;
2688 NEW_LOCLOAD (cfg, ins, n);
2689 ins->cil_code = ip++;
2697 n = (*ip)-CEE_STLOC_0;
2699 handle_loaded_temps (cfg, bblock, stack_start, sp);
2700 NEW_LOCSTORE (cfg, ins, n, *sp);
2702 if (ins->opcode == CEE_STOBJ) {
2703 NEW_LOCLOADA (cfg, ins, n);
2704 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2706 MONO_ADD_INS (bblock, ins);
2711 CHECK_STACK_OVF (1);
2712 NEW_ARGLOAD (cfg, ins, ip [1]);
2718 CHECK_STACK_OVF (1);
2719 NEW_ARGLOADA (cfg, ins, ip [1]);
2727 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2728 handle_loaded_temps (cfg, bblock, stack_start, sp);
2730 if (ins->opcode == CEE_STOBJ) {
2731 NEW_ARGLOADA (cfg, ins, ip [1]);
2732 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2734 MONO_ADD_INS (bblock, ins);
2738 CHECK_STACK_OVF (1);
2739 NEW_LOCLOAD (cfg, ins, ip [1]);
2745 CHECK_STACK_OVF (1);
2746 NEW_LOCLOADA (cfg, ins, ip [1]);
2754 handle_loaded_temps (cfg, bblock, stack_start, sp);
2755 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
2757 if (ins->opcode == CEE_STOBJ) {
2758 NEW_LOCLOADA (cfg, ins, ip [1]);
2759 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2761 MONO_ADD_INS (bblock, ins);
2766 CHECK_STACK_OVF (1);
2767 NEW_PCONST (cfg, ins, NULL);
2769 ins->type = STACK_OBJ;
2774 CHECK_STACK_OVF (1);
2775 NEW_ICONST (cfg, ins, -1);
2789 CHECK_STACK_OVF (1);
2790 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
2796 CHECK_STACK_OVF (1);
2798 NEW_ICONST (cfg, ins, *((signed char*)ip));
2804 CHECK_STACK_OVF (1);
2805 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
2811 CHECK_STACK_OVF (1);
2812 MONO_INST_NEW (cfg, ins, OP_I8CONST);
2814 ins->type = STACK_I8;
2816 ins->inst_l = (gint64)read64 (ip);
2821 float *f = g_malloc (sizeof (float));
2822 CHECK_STACK_OVF (1);
2823 MONO_INST_NEW (cfg, ins, OP_R4CONST);
2824 ins->type = STACK_R8;
2833 double *d = g_malloc (sizeof (double));
2834 CHECK_STACK_OVF (1);
2835 MONO_INST_NEW (cfg, ins, OP_R8CONST);
2836 ins->type = STACK_R8;
2845 MonoInst *temp, *store;
2847 CHECK_STACK_OVF (1);
2852 * small optimization: if the loaded value was from a local already,
2853 * just load it twice.
2855 if (ins->ssa_op == MONO_SSA_LOAD &&
2856 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
2858 MONO_INST_NEW (cfg, temp, 0);
2860 temp->cil_code = ip;
2863 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2864 temp->cil_code = ip;
2865 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2866 store->cil_code = ip;
2867 MONO_ADD_INS (bblock, store);
2868 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2871 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2881 MONO_INST_NEW (cfg, ins, CEE_POP);
2882 MONO_ADD_INS (bblock, ins);
2883 ins->cil_code = ip++;
2888 if (stack_start != sp)
2890 MONO_INST_NEW (cfg, ins, CEE_JMP);
2891 token = read32 (ip + 1);
2892 /* FIXME: check the signature matches */
2893 cmethod = mono_get_method (image, token, NULL);
2894 ins->inst_p0 = cmethod;
2895 MONO_ADD_INS (bblock, ins);
2897 start_new_bblock = 1;
2901 case CEE_CALLVIRT: {
2902 MonoInst *addr = NULL;
2903 MonoMethodSignature *fsig = NULL;
2904 int temp, array_rank = 0;
2905 int virtual = *ip == CEE_CALLVIRT;
2907 token = read32 (ip + 1);
2909 if (*ip == CEE_CALLI) {
2914 if (method->wrapper_type != MONO_WRAPPER_NONE)
2915 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
2917 fsig = mono_metadata_parse_signature (image, token);
2919 n = fsig->param_count + fsig->hasthis;
2922 if (method->wrapper_type != MONO_WRAPPER_NONE) {
2923 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
2925 cmethod = mono_get_method (image, token, NULL);
2928 if (!cmethod->klass->inited)
2929 mono_class_init (cmethod->klass);
2931 if (cmethod->signature->pinvoke) {
2932 #ifdef MONO_USE_EXC_TABLES
2933 if (mono_method_blittable (cmethod)) {
2934 fsig = cmethod->signature;
2937 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
2938 fsig = wrapper->signature;
2939 #ifdef MONO_USE_EXC_TABLES
2943 fsig = mono_method_get_signature (cmethod, image, token);
2946 n = fsig->param_count + fsig->hasthis;
2948 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
2949 cmethod->klass->parent == mono_defaults.array_class) {
2950 array_rank = cmethod->klass->rank;
2953 if (cmethod->string_ctor)
2954 g_assert_not_reached ();
2960 //g_assert (!virtual || fsig->hasthis);
2964 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
2967 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL)) {
2969 for (i = 0; i < n; ++i) {
2970 NEW_ARGSTORE (cfg, ins, i, sp [i]);
2972 MONO_ADD_INS (bblock, ins);
2974 MONO_INST_NEW (cfg, ins, CEE_JMP);
2976 ins->inst_p0 = cmethod;
2977 MONO_ADD_INS (bblock, ins);
2978 start_new_bblock = 1;
2979 /* skip CEE_RET as well */
2984 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
2987 if (MONO_TYPE_IS_VOID (fsig->ret)) {
2988 MONO_ADD_INS (bblock, ins);
2990 type_to_eval_stack_type (fsig->ret, ins);
2999 handle_loaded_temps (cfg, bblock, stack_start, sp);
3001 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3002 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
3003 mono_method_check_inlining (cfg, cmethod) &&
3004 !g_list_find (dont_inline, cmethod)) {
3006 MonoBasicBlock *ebblock;
3008 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3012 GET_BBLOCK (cfg, bbhash, bblock, ip);
3013 ebblock->next_bb = bblock;
3014 link_bblock (cfg, ebblock, bblock);
3016 if (!MONO_TYPE_IS_VOID (fsig->ret))
3019 /* indicates start of a new block, and triggers a load of all
3020 stack arguments at bb boundarie */
3023 inline_costs += costs;
3028 inline_costs += 10 * num_calls++;
3030 /* tail recursion elimination */
3031 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == cfg->method && ip [5] == CEE_RET) {
3032 gboolean has_vtargs = FALSE;
3035 /* keep it simple */
3036 for (i = fsig->param_count - 1; i >= 0; i--) {
3037 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3042 for (i = 0; i < n; ++i) {
3043 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3045 MONO_ADD_INS (bblock, ins);
3047 MONO_INST_NEW (cfg, ins, CEE_BR);
3049 MONO_ADD_INS (bblock, ins);
3050 tblock = start_bblock->out_bb [0];
3051 link_bblock (cfg, bblock, tblock);
3052 ins->inst_target_bb = tblock;
3053 start_new_bblock = 1;
3056 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3057 /* just create a dummy - the value is never used */
3058 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3059 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3067 if (*ip == CEE_CALLI) {
3069 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3070 NEW_TEMPLOAD (cfg, *sp, temp);
3074 } else if (array_rank) {
3077 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3078 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3079 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3081 if (ins->opcode == CEE_STOBJ) {
3082 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3084 MONO_ADD_INS (bblock, ins);
3087 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3088 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3089 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3093 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3094 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3097 g_assert_not_reached ();
3101 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3102 /* no need to spill */
3103 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3106 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3107 NEW_TEMPLOAD (cfg, *sp, temp);
3117 if (cfg->method != method) {
3118 /* return from inlined methode */
3123 //g_assert (returnvar != -1);
3124 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3125 store->cil_code = sp [0]->cil_code;
3126 if (store->opcode == CEE_STOBJ) {
3127 g_assert_not_reached ();
3128 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3129 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3131 MONO_ADD_INS (bblock, store);
3135 g_assert (!return_var);
3138 MONO_INST_NEW (cfg, ins, CEE_NOP);
3139 ins->opcode = mono_type_to_stind (method->signature->ret);
3140 if (ins->opcode == CEE_STOBJ) {
3141 NEW_RETLOADA (cfg, ins);
3142 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3144 ins->opcode = OP_SETRET;
3146 ins->inst_i0 = *sp;;
3147 ins->inst_i1 = NULL;
3148 MONO_ADD_INS (bblock, ins);
3152 if (sp != stack_start)
3154 MONO_INST_NEW (cfg, ins, CEE_BR);
3155 ins->cil_code = ip++;
3156 ins->inst_target_bb = end_bblock;
3157 MONO_ADD_INS (bblock, ins);
3158 link_bblock (cfg, bblock, end_bblock);
3159 start_new_bblock = 1;
3162 MONO_INST_NEW (cfg, ins, CEE_BR);
3163 ins->cil_code = ip++;
3164 MONO_ADD_INS (bblock, ins);
3165 target = ip + 1 + (signed char)(*ip);
3167 GET_BBLOCK (cfg, bbhash, tblock, target);
3168 link_bblock (cfg, bblock, tblock);
3169 CHECK_BBLOCK (target, ip, tblock);
3170 ins->inst_target_bb = tblock;
3171 if (sp != stack_start) {
3172 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3175 start_new_bblock = 1;
3181 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3182 ins->cil_code = ip++;
3183 target = ip + 1 + *(signed char*)ip;
3185 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3186 if (sp != stack_start) {
3187 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3203 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3204 ins->cil_code = ip++;
3205 target = ip + 1 + *(signed char*)ip;
3208 if (sp != stack_start) {
3209 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3215 MONO_INST_NEW (cfg, ins, CEE_BR);
3216 ins->cil_code = ip++;
3217 MONO_ADD_INS (bblock, ins);
3218 target = ip + 4 + (gint32)read32(ip);
3220 GET_BBLOCK (cfg, bbhash, tblock, target);
3221 link_bblock (cfg, bblock, tblock);
3222 CHECK_BBLOCK (target, ip, tblock);
3223 ins->inst_target_bb = tblock;
3224 if (sp != stack_start) {
3225 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3228 start_new_bblock = 1;
3234 MONO_INST_NEW (cfg, ins, *ip);
3235 ins->cil_code = ip++;
3236 target = ip + 4 + (gint32)read32(ip);
3238 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3239 if (sp != stack_start) {
3240 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3256 MONO_INST_NEW (cfg, ins, *ip);
3257 ins->cil_code = ip++;
3258 target = ip + 4 + (gint32)read32(ip);
3261 if (sp != stack_start) {
3262 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3269 n = read32 (ip + 1);
3270 MONO_INST_NEW (cfg, ins, *ip);
3272 ins->inst_left = *sp;
3273 if (ins->inst_left->type != STACK_I4) goto unverified;
3276 target = ip + n * sizeof (guint32);
3277 MONO_ADD_INS (bblock, ins);
3278 GET_BBLOCK (cfg, bbhash, tblock, target);
3279 link_bblock (cfg, bblock, tblock);
3280 ins->klass = GUINT_TO_POINTER (n);
3281 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3282 ins->inst_many_bb [n] = tblock;
3284 for (i = 0; i < n; ++i) {
3285 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3286 link_bblock (cfg, bblock, tblock);
3287 ins->inst_many_bb [i] = tblock;
3290 /* FIXME: handle stack args */
3305 MONO_INST_NEW (cfg, ins, *ip);
3310 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3311 ins->flags |= ins_flag;
3323 MONO_INST_NEW (cfg, ins, *ip);
3324 ins->cil_code = ip++;
3326 handle_loaded_temps (cfg, bblock, stack_start, sp);
3327 MONO_ADD_INS (bblock, ins);
3328 ins->inst_i0 = sp [0];
3329 ins->inst_i1 = sp [1];
3330 ins->flags |= ins_flag;
3361 case CEE_CONV_OVF_I8:
3362 case CEE_CONV_OVF_U8:
3368 case CEE_CONV_OVF_I4:
3369 case CEE_CONV_OVF_I1:
3370 case CEE_CONV_OVF_I2:
3371 case CEE_CONV_OVF_I:
3372 case CEE_CONV_OVF_U:
3375 if (sp [-1]->type == STACK_R8) {
3376 ADD_UNOP (CEE_CONV_OVF_I8);
3384 case CEE_CONV_OVF_U1:
3385 case CEE_CONV_OVF_U2:
3386 case CEE_CONV_OVF_U4:
3389 if (sp [-1]->type == STACK_R8) {
3390 ADD_UNOP (CEE_CONV_OVF_U8);
3398 case CEE_CONV_OVF_I1_UN:
3399 case CEE_CONV_OVF_I2_UN:
3400 case CEE_CONV_OVF_I4_UN:
3401 case CEE_CONV_OVF_I8_UN:
3402 case CEE_CONV_OVF_U1_UN:
3403 case CEE_CONV_OVF_U2_UN:
3404 case CEE_CONV_OVF_U4_UN:
3405 case CEE_CONV_OVF_U8_UN:
3406 case CEE_CONV_OVF_I_UN:
3407 case CEE_CONV_OVF_U_UN:
3413 g_error ("opcode 0x%02x not handled", *ip);
3416 MonoInst *iargs [3];
3419 token = read32 (ip + 1);
3420 if (method->wrapper_type != MONO_WRAPPER_NONE)
3421 klass = mono_method_get_wrapper_data (method, token);
3423 klass = mono_class_get (image, token);
3425 mono_class_init (klass);
3426 n = mono_class_value_size (klass, NULL);
3427 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3428 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3429 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3431 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3432 copy->inst_left = iargs [0];
3433 copy->inst_right = *sp;
3434 copy->cil_code = ip;
3436 MONO_ADD_INS (bblock, copy);
3439 NEW_ICONST (cfg, iargs [2], n);
3440 iargs [2]->cil_code = ip;
3442 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3444 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3451 CHECK_STACK_OVF (1);
3452 n = read32 (ip + 1);
3454 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3456 MonoInst *iargs [1];
3458 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3459 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3460 NEW_TEMPLOAD (cfg, *sp, temp);
3464 if (mono_compile_aot) {
3465 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3468 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
3470 MonoInst *iargs [3];
3471 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3472 NEW_IMAGECONST (cfg, iargs [1], image);
3473 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3474 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3475 NEW_TEMPLOAD (cfg, *sp, temp);
3476 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3478 NEW_PCONST (cfg, ins, NULL);
3480 ins->type = STACK_OBJ;
3481 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3490 MonoInst *iargs [2];
3491 MonoMethodSignature *fsig;
3494 token = read32 (ip + 1);
3495 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3496 cmethod = mono_method_get_wrapper_data (method, token);
3498 cmethod = mono_get_method (image, token, NULL);
3499 fsig = mono_method_get_signature (cmethod, image, token);
3501 mono_class_init (cmethod->klass);
3503 n = fsig->param_count;
3506 /* move the args to allow room for 'this' in the first position */
3512 handle_loaded_temps (cfg, bblock, stack_start, sp);
3515 if (cmethod->klass->parent == mono_defaults.array_class) {
3516 NEW_METHODCONST (cfg, *sp, cmethod);
3517 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
3519 } else if (cmethod->string_ctor) {
3520 /* we simply pass a null pointer */
3521 NEW_PCONST (cfg, *sp, NULL);
3522 /* now call the string ctor */
3523 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
3525 if (cmethod->klass->valuetype) {
3526 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3527 temp = iargs [0]->inst_c0;
3528 NEW_TEMPLOADA (cfg, *sp, temp);
3530 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
3531 NEW_DOMAINCONST (cfg, iargs [0]);
3532 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3534 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3536 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3537 NEW_PCONST (cfg, iargs [0], vtable);
3538 if (cmethod->klass->has_finalize || cmethod->klass->marshalbyref || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
3539 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3541 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
3543 NEW_TEMPLOAD (cfg, *sp, temp);
3546 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3547 mono_method_check_inlining (cfg, cmethod) &&
3548 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
3549 !g_list_find (dont_inline, cmethod)) {
3551 MonoBasicBlock *ebblock;
3552 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3557 GET_BBLOCK (cfg, bbhash, bblock, ip);
3558 ebblock->next_bb = bblock;
3559 link_bblock (cfg, ebblock, bblock);
3561 NEW_TEMPLOAD (cfg, *sp, temp);
3564 /* indicates start of a new block, and triggers a load
3565 of all stack arguments at bb boundarie */
3568 inline_costs += costs;
3572 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3575 /* now call the actual ctor */
3576 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3580 NEW_TEMPLOAD (cfg, *sp, temp);
3589 MONO_INST_NEW (cfg, ins, *ip);
3591 klass = mono_class_get (image, read32 (ip + 1));
3592 mono_class_init (klass);
3593 ins->type = STACK_OBJ;
3594 ins->inst_left = *sp;
3595 ins->inst_newa_class = klass;
3601 MonoInst *add, *vtoffset;
3605 token = read32 (ip + 1);
3606 if (method->wrapper_type != MONO_WRAPPER_NONE)
3607 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3609 klass = mono_class_get (image, token);
3610 mono_class_init (klass);
3613 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
3614 ins->type = STACK_OBJ;
3615 ins->inst_left = *sp;
3617 ins->inst_newa_class = klass;
3620 MONO_INST_NEW (cfg, add, CEE_ADD);
3621 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3622 add->inst_left = ins;
3623 add->inst_right = vtoffset;
3624 add->type = STACK_MP;
3632 MONO_INST_NEW (cfg, ins, *ip);
3634 klass = mono_class_get (image, read32 (ip + 1));
3635 mono_class_init (klass);
3636 ins->type = STACK_OBJ;
3637 ins->inst_left = *sp;
3639 ins->inst_newa_class = klass;
3646 MONO_INST_NEW (cfg, ins, *ip);
3648 ins->inst_left = *sp;
3649 ins->cil_code = ip++;
3650 MONO_ADD_INS (bblock, ins);
3652 start_new_bblock = 1;
3657 MonoInst *offset_ins;
3658 MonoClassField *field;
3659 MonoBasicBlock *ebblock;
3663 if (*ip == CEE_STFLD) {
3670 // FIXME: enable this test later.
3671 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
3673 token = read32 (ip + 1);
3674 field = mono_field_from_token (image, token, &klass);
3675 mono_class_init (klass);
3677 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
3678 /* FIXME: mark instructions for use in SSA */
3679 if (*ip == CEE_STFLD) {
3680 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3681 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
3682 MonoInst *iargs [5];
3685 NEW_CLASSCONST (cfg, iargs [1], klass);
3686 NEW_FIELDCONST (cfg, iargs [2], field);
3687 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
3691 if (cfg->opt & MONO_OPT_INLINE) {
3692 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
3693 iargs, ip, real_offset, dont_inline, &ebblock);
3694 g_assert (costs > 0);
3699 GET_BBLOCK (cfg, bbhash, bblock, ip);
3700 ebblock->next_bb = bblock;
3701 link_bblock (cfg, ebblock, bblock);
3703 /* indicates start of a new block, and triggers a load
3704 of all stack arguments at bb boundarie */
3707 inline_costs += costs;
3710 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
3714 NEW_ICONST (cfg, offset_ins, foffset);
3715 MONO_INST_NEW (cfg, ins, CEE_ADD);
3717 ins->inst_left = *sp;
3718 ins->inst_right = offset_ins;
3719 ins->type = STACK_MP;
3721 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3722 store->cil_code = ip;
3723 store->inst_left = ins;
3724 store->inst_right = sp [1];
3725 handle_loaded_temps (cfg, bblock, stack_start, sp);
3726 store->flags |= ins_flag;
3728 if (store->opcode == CEE_STOBJ) {
3729 handle_stobj (cfg, bblock, ins, sp [1], ip,
3730 mono_class_from_mono_type (field->type), FALSE, FALSE);
3732 MONO_ADD_INS (bblock, store);
3735 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3736 /* fixme: we need to inline that call somehow */
3737 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
3738 MonoInst *iargs [4];
3742 NEW_CLASSCONST (cfg, iargs [1], klass);
3743 NEW_FIELDCONST (cfg, iargs [2], field);
3744 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
3745 if (cfg->opt & MONO_OPT_INLINE) {
3746 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
3747 iargs, ip, real_offset, dont_inline, &ebblock);
3748 g_assert (costs > 0);
3753 GET_BBLOCK (cfg, bbhash, bblock, ip);
3754 ebblock->next_bb = bblock;
3755 link_bblock (cfg, ebblock, bblock);
3757 temp = iargs [0]->inst_i0->inst_c0;
3759 if (*ip == CEE_LDFLDA) {
3760 /* not sure howto handle this */
3761 NEW_TEMPLOADA (cfg, *sp, temp);
3763 NEW_TEMPLOAD (cfg, *sp, temp);
3767 /* indicates start of a new block, and triggers a load of
3768 all stack arguments at bb boundarie */
3771 inline_costs += costs;
3774 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
3775 if (*ip == CEE_LDFLDA) {
3776 /* not sure howto handle this */
3777 NEW_TEMPLOADA (cfg, *sp, temp);
3779 NEW_TEMPLOAD (cfg, *sp, temp);
3784 NEW_ICONST (cfg, offset_ins, foffset);
3785 MONO_INST_NEW (cfg, ins, CEE_ADD);
3787 ins->inst_left = *sp;
3788 ins->inst_right = offset_ins;
3789 ins->type = STACK_MP;
3791 if (*ip == CEE_LDFLDA) {
3795 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3796 type_to_eval_stack_type (field->type, load);
3797 load->cil_code = ip;
3798 load->inst_left = ins;
3799 load->flags |= ins_flag;
3811 MonoClassField *field;
3813 token = read32 (ip + 1);
3815 field = mono_field_from_token (image, token, &klass);
3816 mono_class_init (klass);
3818 handle_loaded_temps (cfg, bblock, stack_start, sp);
3820 if (((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)) {
3822 MonoInst *iargs [2];
3823 g_assert (field->parent);
3824 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3825 NEW_FIELDCONST (cfg, iargs [1], field);
3826 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
3827 NEW_TEMPLOAD (cfg, ins, temp);
3831 vtable = mono_class_vtable (cfg->domain, klass);
3832 if (!cfg->domain->thread_static_fields || !(addr = g_hash_table_lookup (cfg->domain->thread_static_fields, field))) {
3833 if (!vtable->initialized && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && needs_cctor_run (klass, method)) {
3834 MonoInst *iargs [1];
3835 NEW_PCONST (cfg, iargs [0], vtable);
3836 mono_emit_jit_icall (cfg, bblock, mono_runtime_class_init, iargs, ip);
3837 if (cfg->verbose_level > 2)
3838 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
3840 mono_runtime_class_init (vtable);
3842 addr = (char*)vtable->data + field->offset;
3843 NEW_PCONST (cfg, ins, addr);
3847 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
3848 * This could be later optimized to do just a couple of
3849 * memory dereferences with constant offsets.
3852 MonoInst *iargs [1];
3853 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
3854 temp = mono_emit_jit_icall (cfg, bblock, mono_threads_get_static_data, iargs, ip);
3855 NEW_TEMPLOAD (cfg, ins, temp);
3859 /* FIXME: mark instructions for use in SSA */
3860 if (*ip == CEE_LDSFLDA) {
3862 } else if (*ip == CEE_STSFLD) {
3866 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3867 store->cil_code = ip;
3868 store->inst_left = ins;
3869 store->inst_right = sp [0];
3870 store->flags |= ins_flag;
3873 if (store->opcode == CEE_STOBJ) {
3874 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
3876 MONO_ADD_INS (bblock, store);
3878 gboolean is_const = FALSE;
3879 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3880 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
3881 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
3882 gpointer addr = (char*)vtable->data + field->offset;
3883 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
3885 switch (field->type->type) {
3886 case MONO_TYPE_BOOLEAN:
3888 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
3892 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
3895 case MONO_TYPE_CHAR:
3897 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
3901 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
3906 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
3910 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
3915 case MONO_TYPE_STRING:
3916 case MONO_TYPE_OBJECT:
3917 case MONO_TYPE_CLASS:
3918 case MONO_TYPE_SZARRAY:
3920 case MONO_TYPE_FNPTR:
3921 case MONO_TYPE_ARRAY:
3922 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
3923 type_to_eval_stack_type (field->type, *sp);
3928 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
3929 sp [0]->type = STACK_I8;
3930 sp [0]->inst_l = *((gint64 *)addr);
3935 case MONO_TYPE_VALUETYPE:
3944 CHECK_STACK_OVF (1);
3945 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3946 type_to_eval_stack_type (field->type, load);
3947 load->cil_code = ip;
3948 load->inst_left = ins;
3950 load->flags |= ins_flag;
3952 /* fixme: dont see the problem why this does not work */
3953 //cfg->disable_aot = TRUE;
3962 token = read32 (ip + 1);
3963 if (method->wrapper_type != MONO_WRAPPER_NONE)
3964 klass = mono_method_get_wrapper_data (method, token);
3966 klass = mono_class_get (image, token);
3967 mono_class_init (klass);
3968 n = mono_type_to_stind (&klass->byval_arg);
3969 if (n == CEE_STOBJ) {
3970 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
3972 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
3974 MONO_INST_NEW (cfg, store, n);
3975 store->cil_code = ip;
3976 store->inst_left = sp [0];
3977 store->inst_right = sp [1];
3978 store->flags |= ins_flag;
3979 MONO_ADD_INS (bblock, store);
3986 MonoInst *iargs [2];
3987 MonoInst *load, *vtoffset, *add, *val, *vstore;
3992 token = read32 (ip + 1);
3993 if (method->wrapper_type != MONO_WRAPPER_NONE)
3994 klass = mono_method_get_wrapper_data (method, token);
3996 klass = mono_class_get (image, token);
3997 mono_class_init (klass);
3999 /* much like NEWOBJ */
4000 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
4001 NEW_DOMAINCONST (cfg, iargs [0]);
4002 NEW_CLASSCONST (cfg, iargs [1], klass);
4004 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4006 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4007 NEW_PCONST (cfg, iargs [0], vtable);
4008 if (1 || klass->has_finalize || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
4009 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
4011 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
4013 NEW_TEMPLOAD (cfg, load, temp);
4014 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4015 MONO_INST_NEW (cfg, add, CEE_ADD);
4016 add->inst_left = load;
4017 add->inst_right = vtoffset;
4020 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
4021 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
4022 vstore->cil_code = ip;
4023 vstore->inst_left = add;
4024 vstore->inst_right = val;
4026 if (vstore->opcode == CEE_STOBJ) {
4027 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
4029 MONO_ADD_INS (bblock, vstore);
4031 NEW_TEMPLOAD (cfg, load, temp);
4039 MONO_INST_NEW (cfg, ins, *ip);
4043 token = read32 (ip + 1);
4045 /* allocate the domainvar - becaus this is used in decompose_foreach */
4046 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)
4047 mono_get_domainvar (cfg);
4049 if (method->wrapper_type != MONO_WRAPPER_NONE)
4050 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4052 klass = mono_class_get (image, token);
4054 mono_class_init (klass);
4055 ins->inst_newa_class = klass;
4056 ins->inst_newa_len = *sp;
4057 ins->type = STACK_OBJ;
4064 MONO_INST_NEW (cfg, ins, *ip);
4065 ins->cil_code = ip++;
4067 ins->inst_left = *sp;
4068 ins->type = STACK_PTR;
4074 klass = mono_class_get (image, read32 (ip + 1));
4075 mono_class_init (klass);
4076 NEW_LDELEMA (cfg, ins, sp, klass);
4091 case CEE_LDELEM_REF: {
4095 * ldind.x (ldelema (array, index))
4096 * ldelema does the bounds check
4100 klass = array_access_to_klass (*ip);
4101 NEW_LDELEMA (cfg, load, sp, klass);
4102 load->cil_code = ip;
4103 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4105 ins->inst_left = load;
4107 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4117 case CEE_STELEM_R8: {
4121 * stind.x (ldelema (array, index), val)
4122 * ldelema does the bounds check
4126 klass = array_access_to_klass (*ip);
4127 NEW_LDELEMA (cfg, load, sp, klass);
4128 load->cil_code = ip;
4129 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4131 ins->inst_left = load;
4132 ins->inst_right = sp [2];
4134 handle_loaded_temps (cfg, bblock, stack_start, sp);
4135 MONO_ADD_INS (bblock, ins);
4136 /* FIXME: add the implicit STELEM_REF castclass */
4138 cfg->disable_ssa = TRUE;
4141 case CEE_STELEM_REF: {
4142 MonoInst *iargs [3];
4147 handle_loaded_temps (cfg, bblock, stack_start, sp);
4153 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4157 NEW_GROUP (cfg, group, sp [0], sp [1]);
4158 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4160 ins->inst_left = group;
4161 ins->inst_right = sp [2];
4162 MONO_ADD_INS (bblock, ins);
4167 cfg->disable_ssa = TRUE;
4170 case CEE_CKFINITE: {
4171 MonoInst *store, *temp;
4174 /* this instr. can throw exceptions as side effect,
4175 * so we cant eliminate dead code which contains CKFINITE opdodes.
4176 * Spilling to memory makes sure that we always perform
4180 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4182 ins->inst_left = sp [-1];
4183 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4185 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4186 store->cil_code = ip;
4187 MONO_ADD_INS (bblock, store);
4189 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4196 g_error ("opcode 0x%02x not handled", *ip);
4200 MonoClass *handle_class;
4202 CHECK_STACK_OVF (1);
4204 n = read32 (ip + 1);
4206 handle = mono_ldtoken (image, n, &handle_class);
4207 mono_class_init (handle_class);
4209 if (((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)) {
4211 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4213 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4215 NEW_IMAGECONST (cfg, iargs [0], image);
4216 NEW_ICONST (cfg, iargs [1], n);
4217 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4218 NEW_TEMPLOAD (cfg, res, temp);
4219 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4220 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4221 MONO_ADD_INS (bblock, store);
4222 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4224 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method (image, read32 (ip + 6), NULL)) &&
4225 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4226 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
4227 MonoClass *tclass = mono_class_from_mono_type (handle);
4228 mono_class_init (tclass);
4229 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4230 ins->type = STACK_OBJ;
4231 ins->klass = cmethod->klass;
4234 NEW_PCONST (cfg, ins, handle);
4235 ins->type = STACK_VTYPE;
4236 ins->klass = handle_class;
4252 case CEE_ADD_OVF_UN:
4254 case CEE_MUL_OVF_UN:
4256 case CEE_SUB_OVF_UN:
4261 case CEE_ENDFINALLY:
4262 /* FIXME: check stack state */
4263 MONO_INST_NEW (cfg, ins, *ip);
4264 MONO_ADD_INS (bblock, ins);
4265 ins->cil_code = ip++;
4266 start_new_bblock = 1;
4271 if (*ip == CEE_LEAVE) {
4272 target = ip + 5 + (gint32)read32(ip + 1);
4274 target = ip + 2 + (signed char)(ip [1]);
4277 /* empty the stack */
4278 while (sp != stack_start) {
4279 MONO_INST_NEW (cfg, ins, CEE_POP);
4283 MONO_ADD_INS (bblock, ins);
4286 /* fixme: call fault handler ? */
4288 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
4290 for (tmp = handlers; tmp; tmp = tmp->next) {
4292 link_bblock (cfg, bblock, tblock);
4293 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
4295 ins->inst_target_bb = tblock;
4296 MONO_ADD_INS (bblock, ins);
4298 g_list_free (handlers);
4301 MONO_INST_NEW (cfg, ins, CEE_BR);
4303 MONO_ADD_INS (bblock, ins);
4304 GET_BBLOCK (cfg, bbhash, tblock, target);
4305 link_bblock (cfg, bblock, tblock);
4306 CHECK_BBLOCK (target, ip, tblock);
4307 ins->inst_target_bb = tblock;
4308 start_new_bblock = 1;
4310 if (*ip == CEE_LEAVE)
4319 MONO_INST_NEW (cfg, ins, *ip);
4321 handle_loaded_temps (cfg, bblock, stack_start, sp);
4322 MONO_ADD_INS (bblock, ins);
4323 ins->cil_code = ip++;
4324 ins->inst_i0 = sp [0];
4325 ins->inst_i1 = sp [1];
4333 /* trampoline mono specific opcodes */
4334 case MONO_CUSTOM_PREFIX: {
4336 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
4340 case CEE_MONO_FUNC1: {
4342 gpointer func = NULL;
4347 case MONO_MARSHAL_CONV_STR_LPWSTR:
4348 func = mono_string_to_utf16;
4350 case MONO_MARSHAL_CONV_LPWSTR_STR:
4351 func = mono_string_from_utf16;
4353 case MONO_MARSHAL_CONV_LPSTR_STR:
4354 func = mono_string_new_wrapper;
4356 case MONO_MARSHAL_CONV_STR_LPTSTR:
4357 case MONO_MARSHAL_CONV_STR_LPSTR:
4358 func = mono_string_to_utf8;
4360 case MONO_MARSHAL_CONV_STR_BSTR:
4361 func = mono_string_to_bstr;
4363 case MONO_MARSHAL_CONV_STR_TBSTR:
4364 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
4365 func = mono_string_to_ansibstr;
4367 case MONO_MARSHAL_CONV_SB_LPSTR:
4368 func = mono_string_builder_to_utf8;
4370 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
4371 func = mono_array_to_savearray;
4373 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
4374 func = mono_array_to_lparray;
4376 case MONO_MARSHAL_CONV_DEL_FTN:
4377 func = mono_delegate_to_ftnptr;
4379 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
4380 func = mono_marshal_string_array;
4383 g_warning ("unknown conversion %d\n", ip [2]);
4384 g_assert_not_reached ();
4387 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4388 NEW_TEMPLOAD (cfg, *sp, temp);
4392 inline_costs += 10 * num_calls++;
4395 case CEE_MONO_PROC2: {
4396 gpointer func = NULL;
4401 case MONO_MARSHAL_CONV_LPSTR_SB:
4402 func = mono_string_utf8_to_builder;
4404 case MONO_MARSHAL_FREE_ARRAY:
4405 func = mono_marshal_free_array;
4408 g_assert_not_reached ();
4411 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4413 inline_costs += 10 * num_calls++;
4416 case CEE_MONO_PROC3: {
4417 gpointer func = NULL;
4422 case MONO_MARSHAL_CONV_STR_BYVALSTR:
4423 func = mono_string_to_byvalstr;
4425 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
4426 func = mono_string_to_byvalwstr;
4429 g_assert_not_reached ();
4432 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4434 inline_costs += 10 * num_calls++;
4440 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
4442 inline_costs += 10 * num_calls++;
4444 case CEE_MONO_LDPTR:
4445 CHECK_STACK_OVF (1);
4446 token = read32 (ip + 2);
4447 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
4451 inline_costs += 10 * num_calls++;
4453 case CEE_MONO_VTADDR:
4456 MONO_INST_NEW (cfg, ins, OP_VTADDR);
4458 ins->type = STACK_MP;
4459 ins->inst_left = *sp;
4463 case CEE_MONO_NEWOBJ: {
4464 MonoInst *iargs [2];
4466 CHECK_STACK_OVF (1);
4467 token = read32 (ip + 2);
4468 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4469 mono_class_init (klass);
4470 NEW_DOMAINCONST (cfg, iargs [0]);
4471 NEW_CLASSCONST (cfg, iargs [1], klass);
4472 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4473 NEW_TEMPLOAD (cfg, *sp, temp);
4476 inline_costs += 10 * num_calls++;
4479 case CEE_MONO_OBJADDR:
4482 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
4484 ins->type = STACK_MP;
4485 ins->inst_left = *sp;
4489 case CEE_MONO_LDNATIVEOBJ:
4491 token = read32 (ip + 2);
4492 klass = mono_method_get_wrapper_data (method, token);
4493 g_assert (klass->valuetype);
4494 mono_class_init (klass);
4495 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
4499 case CEE_MONO_RETOBJ:
4500 g_assert (cfg->ret);
4501 g_assert (method->signature->pinvoke);
4505 token = read32 (ip + 2);
4506 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4508 NEW_RETLOADA (cfg, ins);
4509 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
4511 if (sp != stack_start)
4514 MONO_INST_NEW (cfg, ins, CEE_BR);
4516 ins->inst_target_bb = end_bblock;
4517 MONO_ADD_INS (bblock, ins);
4518 link_bblock (cfg, bblock, end_bblock);
4519 start_new_bblock = 1;
4523 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
4531 /* somewhat similar to LDTOKEN */
4532 MonoInst *addr, *vtvar;
4533 CHECK_STACK_OVF (1);
4534 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
4536 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4537 addr->cil_code = ip;
4538 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
4540 ins->inst_left = addr;
4541 MONO_ADD_INS (bblock, ins);
4542 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4555 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
4556 MONO_INST_NEW (cfg, ins, cmp->opcode);
4558 cmp->inst_i0 = sp [0];
4559 cmp->inst_i1 = sp [1];
4563 cmp->opcode = OP_COMPARE;
4565 ins->type = STACK_I4;
4575 CHECK_STACK_OVF (1);
4576 n = read32 (ip + 2);
4577 if (method->wrapper_type != MONO_WRAPPER_NONE)
4578 cmethod = mono_method_get_wrapper_data (method, n);
4580 cmethod = mono_get_method (image, n, NULL);
4583 * We can't do this in mono_ldftn, since it is used in
4584 * the synchronized wrapper, leading to an infinite loop.
4586 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
4587 cmethod = mono_marshal_get_synchronized_wrapper (cmethod);
4590 mono_class_init (cmethod->klass);
4591 handle_loaded_temps (cfg, bblock, stack_start, sp);
4593 NEW_METHODCONST (cfg, argconst, cmethod);
4594 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
4595 NEW_TEMPLOAD (cfg, *sp, temp);
4599 inline_costs += 10 * num_calls++;
4602 case CEE_LDVIRTFTN: {
4607 n = read32 (ip + 2);
4608 if (method->wrapper_type != MONO_WRAPPER_NONE)
4609 cmethod = mono_method_get_wrapper_data (method, n);
4611 cmethod = mono_get_method (image, n, NULL);
4613 mono_class_init (cmethod->klass);
4614 handle_loaded_temps (cfg, bblock, stack_start, sp);
4618 NEW_METHODCONST (cfg, args [1], cmethod);
4619 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
4620 NEW_TEMPLOAD (cfg, *sp, temp);
4624 inline_costs += 10 * num_calls++;
4628 CHECK_STACK_OVF (1);
4629 NEW_ARGLOAD (cfg, ins, read16 (ip + 2));
4635 CHECK_STACK_OVF (1);
4636 NEW_ARGLOADA (cfg, ins, read16 (ip + 2));
4644 handle_loaded_temps (cfg, bblock, stack_start, sp);
4645 n = read16 (ip + 2);
4646 NEW_ARGSTORE (cfg, ins, n, *sp);
4648 if (ins->opcode == CEE_STOBJ) {
4649 NEW_ARGLOADA (cfg, ins, n);
4650 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4652 MONO_ADD_INS (bblock, ins);
4656 CHECK_STACK_OVF (1);
4657 NEW_LOCLOAD (cfg, ins, read16 (ip + 2));
4663 CHECK_STACK_OVF (1);
4664 NEW_LOCLOADA (cfg, ins, read16 (ip + 2));
4672 n = read16 (ip + 2);
4673 handle_loaded_temps (cfg, bblock, stack_start, sp);
4674 NEW_LOCSTORE (cfg, ins, n, *sp);
4676 if (ins->opcode == CEE_STOBJ) {
4677 NEW_LOCLOADA (cfg, ins, n);
4678 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4680 MONO_ADD_INS (bblock, ins);
4687 if (sp != stack_start)
4689 MONO_INST_NEW (cfg, ins, 256 + ip [1]);
4690 ins->inst_left = *sp;
4693 if (header->init_locals)
4694 ins->flags |= MONO_INST_INIT;
4698 /* FIXME: set init flag if locals init is set in this method */
4700 case CEE_ENDFILTER: {
4701 MonoExceptionClause *clause, *nearest;
4702 int cc, nearest_num;
4706 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
4708 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
4709 ins->inst_left = *sp;
4711 MONO_ADD_INS (bblock, ins);
4712 start_new_bblock = 1;
4716 for (cc = 0; cc < header->num_clauses; ++cc) {
4717 clause = &header->clauses [cc];
4718 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
4719 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
4725 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
4729 case CEE_UNALIGNED_:
4730 ins_flag |= MONO_INST_UNALIGNED;
4734 ins_flag |= MONO_INST_VOLATILE;
4738 ins_flag |= MONO_INST_TAILCALL;
4744 token = read32 (ip + 2);
4745 if (method->wrapper_type != MONO_WRAPPER_NONE)
4746 klass = mono_method_get_wrapper_data (method, token);
4748 klass = mono_class_get (image, token);
4749 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
4755 MonoInst *iargs [3];
4761 handle_loaded_temps (cfg, bblock, stack_start, sp);
4762 if (ip [1] == CEE_CPBLK) {
4763 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4765 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
4773 /* FIXME: check we are in a catch handler */
4774 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
4775 load->cil_code = ip;
4776 MONO_INST_NEW (cfg, ins, CEE_THROW);
4777 ins->inst_left = load;
4779 MONO_ADD_INS (bblock, ins);
4781 start_new_bblock = 1;
4786 CHECK_STACK_OVF (1);
4787 token = read32 (ip + 2);
4788 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
4789 MonoType *type = mono_type_create_from_typespec (image, token);
4790 token = mono_type_size (type, &align);
4791 mono_metadata_free_type (type);
4793 MonoClass *szclass = mono_class_get (image, token);
4794 mono_class_init (szclass);
4795 token = mono_class_value_size (szclass, &align);
4797 NEW_ICONST (cfg, ins, token);
4802 case CEE_REFANYTYPE:
4803 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4806 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4811 g_error ("opcode 0x%02x not handled", *ip);
4814 if (start_new_bblock != 1)
4817 bblock->cil_length = ip - bblock->cil_code;
4818 bblock->next_bb = end_bblock;
4819 link_bblock (cfg, bblock, end_bblock);
4821 if (cfg->method == method && cfg->domainvar) {
4825 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
4826 call->signature = helper_sig_domain_get;
4827 call->inst.type = STACK_PTR;
4828 call->fptr = mono_domain_get;
4829 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
4831 MONO_ADD_INS (init_localsbb, store);
4834 if (header->init_locals) {
4836 for (i = 0; i < header->num_locals; ++i) {
4837 int t = header->locals [i]->type;
4838 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
4839 t = header->locals [i]->data.klass->enum_basetype->type;
4840 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
4841 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
4842 NEW_ICONST (cfg, ins, 0);
4843 NEW_LOCSTORE (cfg, store, i, ins);
4844 MONO_ADD_INS (init_localsbb, store);
4845 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
4846 MONO_INST_NEW (cfg, ins, OP_I8CONST);
4847 ins->type = STACK_I8;
4849 NEW_LOCSTORE (cfg, store, i, ins);
4850 MONO_ADD_INS (init_localsbb, store);
4851 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
4852 MONO_INST_NEW (cfg, ins, OP_R8CONST);
4853 ins->type = STACK_R8;
4854 ins->inst_p0 = (void*)&r8_0;
4855 NEW_LOCSTORE (cfg, store, i, ins);
4856 MONO_ADD_INS (init_localsbb, store);
4857 } else if (t == MONO_TYPE_VALUETYPE) {
4858 NEW_LOCLOADA (cfg, ins, i);
4859 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
4862 NEW_PCONST (cfg, ins, NULL);
4863 NEW_LOCSTORE (cfg, store, i, ins);
4864 MONO_ADD_INS (init_localsbb, store);
4870 /* resolve backward branches in the middle of an existing basic block */
4871 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
4873 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
4874 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
4875 if (tblock != start_bblock) {
4877 split_bblock (cfg, tblock, bblock);
4878 l = bblock->cil_code - header->code;
4879 bblock->cil_length = tblock->cil_length - l;
4880 tblock->cil_length = l;
4882 g_print ("recheck failed.\n");
4886 /* we compute regions here, because the length of filter clauses is not known in advance.
4887 * It is computed in the CEE_ENDFILTER case in the above switch statement*/
4888 if (cfg->method == method) {
4890 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4891 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
4892 if (cfg->verbose_level > 2)
4893 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
4896 g_hash_table_destroy (bbhash);
4899 dont_inline = g_list_remove (dont_inline, method);
4900 return inline_costs;
4903 if (cfg->method != method)
4904 g_hash_table_destroy (bbhash);
4905 dont_inline = g_list_remove (dont_inline, method);
4909 if (cfg->method != method)
4910 g_hash_table_destroy (bbhash);
4911 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
4912 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
4913 dont_inline = g_list_remove (dont_inline, method);
4918 mono_print_tree (MonoInst *tree) {
4924 arity = mono_burg_arity [tree->opcode];
4926 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
4928 switch (tree->opcode) {
4930 printf ("[%d]", tree->inst_c0);
4933 printf ("[%lld]", tree->inst_l);
4936 printf ("[%f]", *(double*)tree->inst_p0);
4939 printf ("[%f]", *(float*)tree->inst_p0);
4943 printf ("[%d]", tree->inst_c0);
4946 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
4949 printf ("[%s]", mono_arch_regname (tree->dreg));
4952 printf ("[%s]", tree->inst_newa_class->name);
4953 mono_print_tree (tree->inst_newa_len);
4964 case OP_VOIDCALLVIRT: {
4965 MonoCallInst *call = (MonoCallInst*)tree;
4967 printf ("[%s]", call->method->name);
4972 printf ("[%d (", tree->inst_c0);
4973 for (i = 0; i < tree->inst_phi_args [0]; i++) {
4976 printf ("%d", tree->inst_phi_args [i + 1]);
4988 printf ("[B%d]", tree->inst_target_bb->block_num);
4998 case OP_VOIDCALL_REG:
4999 mono_print_tree (tree->inst_left);
5011 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5012 mono_print_tree (tree->inst_left);
5016 mono_print_tree (tree->inst_left);
5018 mono_print_tree (tree->inst_right);
5028 create_helper_signature (void)
5030 /* FIXME: set call conv */
5031 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
5032 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5033 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
5034 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
5035 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
5036 helper_sig_newarr->pinvoke = 1;
5038 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
5039 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5040 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
5041 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
5042 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
5043 helper_sig_newarr_specific->pinvoke = 1;
5045 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
5046 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5047 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
5048 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
5049 helper_sig_object_new->pinvoke = 1;
5051 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
5052 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5053 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
5054 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
5055 helper_sig_object_new_specific->pinvoke = 1;
5057 /* void* mono_method_compile (MonoMethod*) */
5058 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5059 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
5060 helper_sig_compile->pinvoke = 1;
5062 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
5063 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5064 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
5065 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
5066 helper_sig_compile_virt->pinvoke = 1;
5068 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
5069 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5070 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
5071 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
5072 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
5073 helper_sig_ldstr->pinvoke = 1;
5075 /* MonoDomain *mono_domain_get (void) */
5076 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5077 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
5078 helper_sig_domain_get->pinvoke = 1;
5080 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
5081 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5082 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
5083 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
5084 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
5085 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
5086 helper_sig_stelem_ref->pinvoke = 1;
5088 /* long amethod (long, long) */
5089 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5090 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
5091 &mono_defaults.int64_class->byval_arg;
5092 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
5093 helper_sig_long_long_long->pinvoke = 1;
5095 /* object amethod (intptr) */
5096 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5097 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5098 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
5099 helper_sig_obj_ptr->pinvoke = 1;
5101 /* void amethod (intptr) */
5102 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5103 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5104 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
5105 helper_sig_void_ptr->pinvoke = 1;
5107 /* void amethod (MonoObject *obj) */
5108 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5109 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
5110 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
5111 helper_sig_void_obj->pinvoke = 1;
5113 /* intptr amethod (void) */
5114 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5115 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
5116 helper_sig_ptr_void->pinvoke = 1;
5118 /* void amethod (intptr, intptr) */
5119 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5120 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5121 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5122 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5123 helper_sig_void_ptr_ptr->pinvoke = 1;
5125 /* void amethod (intptr, intptr, intptr) */
5126 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5127 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5128 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5129 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
5130 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5131 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
5133 /* intptr amethod (intptr, intptr) */
5134 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5135 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5136 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5137 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
5138 helper_sig_ptr_ptr_ptr->pinvoke = 1;
5140 /* IntPtr amethod (object) */
5141 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5142 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
5143 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
5144 helper_sig_ptr_obj->pinvoke = 1;
5146 /* IntPtr amethod (int) */
5147 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5148 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
5149 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
5150 helper_sig_ptr_int->pinvoke = 1;
5152 /* long amethod (long, guint32) */
5153 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5154 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
5155 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
5156 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
5157 helper_sig_long_long_int->pinvoke = 1;
5159 /* ulong amethod (double) */
5160 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5161 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
5162 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
5163 helper_sig_ulong_double->pinvoke = 1;
5165 /* long amethod (double) */
5166 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5167 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
5168 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
5169 helper_sig_long_double->pinvoke = 1;
5171 /* uint amethod (double) */
5172 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5173 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
5174 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
5175 helper_sig_uint_double->pinvoke = 1;
5177 /* int amethod (double) */
5178 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5179 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
5180 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
5181 helper_sig_int_double->pinvoke = 1;
5183 /* void initobj (intptr, int size) */
5184 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5185 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
5186 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
5187 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
5188 helper_sig_initobj->pinvoke = 1;
5190 /* void memcpy (intptr, intptr, int size) */
5191 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5192 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
5193 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
5194 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
5195 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
5196 helper_sig_memcpy->pinvoke = 1;
5198 /* void memset (intptr, int val, int size) */
5199 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5200 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
5201 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
5202 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
5203 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
5204 helper_sig_memset->pinvoke = 1;
5207 static GHashTable *jit_icall_hash_name = NULL;
5208 static GHashTable *jit_icall_hash_addr = NULL;
5211 mono_find_jit_icall_by_name (const char *name)
5213 g_assert (jit_icall_hash_name);
5215 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
5216 return g_hash_table_lookup (jit_icall_hash_name, name);
5220 mono_find_jit_icall_by_addr (gconstpointer addr)
5222 g_assert (jit_icall_hash_addr);
5224 return g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
5228 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
5231 MonoMethod *wrapper;
5233 if (callinfo->wrapper)
5234 return callinfo->wrapper;
5235 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
5236 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
5237 callinfo->wrapper = mono_jit_compile_method (wrapper);
5239 return callinfo->wrapper;
5243 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
5245 MonoJitICallInfo *info;
5250 if (!jit_icall_hash_name) {
5251 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
5252 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
5255 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
5256 g_warning ("jit icall already defined \"%s\"\n", name);
5257 g_assert_not_reached ();
5260 info = g_new (MonoJitICallInfo, 1);
5267 #ifdef MONO_USE_EXC_TABLES
5268 || mono_arch_has_unwind_info (func)
5271 info->wrapper = func;
5273 info->wrapper = NULL;
5274 mono_icall_get_wrapper (info);
5277 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
5278 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
5279 if (func != info->wrapper)
5280 g_hash_table_insert (jit_icall_hash_addr, (gpointer)info->wrapper, info);
5285 static GHashTable *emul_opcode_hash = NULL;
5287 static MonoJitICallInfo *
5288 mono_find_jit_opcode_emulation (int opcode)
5290 if (emul_opcode_hash)
5291 return g_hash_table_lookup (emul_opcode_hash, (gpointer)opcode);
5297 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
5299 MonoJitICallInfo *info;
5301 if (!emul_opcode_hash)
5302 emul_opcode_hash = g_hash_table_new (NULL, NULL);
5304 g_assert (!sig->hasthis);
5305 g_assert (sig->param_count < 3);
5307 info = mono_register_jit_icall (func, name, sig, no_throw);
5309 g_hash_table_insert (emul_opcode_hash, (gpointer)opcode, info);
5313 decompose_foreach (MonoInst *tree, gpointer data)
5315 static MonoJitICallInfo *newarr_info = NULL;
5316 static MonoJitICallInfo *newarr_specific_info = NULL;
5317 MonoJitICallInfo *info;
5319 switch (tree->opcode) {
5321 MonoCompile *cfg = data;
5322 MonoInst *iargs [3];
5325 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
5326 g_assert (newarr_info);
5327 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
5328 g_assert (newarr_specific_info);
5331 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
5332 NEW_DOMAINCONST (cfg, iargs [0]);
5333 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
5334 iargs [2] = tree->inst_newa_len;
5339 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
5341 NEW_PCONST (cfg, iargs [0], vtable);
5342 iargs [1] = tree->inst_newa_len;
5344 info = newarr_specific_info;
5347 mono_emulate_opcode (cfg, tree, iargs, info);
5357 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
5359 switch (mono_burg_arity [tree->opcode]) {
5362 mono_inst_foreach (tree->inst_left, func, data);
5365 mono_inst_foreach (tree->inst_left, func, data);
5366 mono_inst_foreach (tree->inst_right, func, data);
5369 g_assert_not_reached ();
5376 mono_print_bb_code (MonoBasicBlock *bb) {
5378 MonoInst *c = bb->code;
5380 mono_print_tree (c);
5389 print_dfn (MonoCompile *cfg) {
5394 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
5396 for (i = 0; i < cfg->num_bblocks; ++i) {
5397 bb = cfg->bblocks [i];
5399 char* code1, *code2;
5400 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
5401 if (bb->last_ins->cil_code)
5402 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
5404 code2 = g_strdup ("");
5406 code1 [strlen (code1) - 1] = 0;
5407 code = g_strdup_printf ("%s -> %s", code1, code2);
5411 code = g_strdup ("\n");
5412 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
5414 MonoInst *c = bb->code;
5416 mono_print_tree (c);
5424 g_print ("\tprev:");
5425 for (j = 0; j < bb->in_count; ++j) {
5426 g_print (" BB%d", bb->in_bb [j]->block_num);
5428 g_print ("\t\tsucc:");
5429 for (j = 0; j < bb->out_count; ++j) {
5430 g_print (" BB%d", bb->out_bb [j]->block_num);
5432 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
5435 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
5438 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
5440 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
5448 * returns the offset used by spillvar. It allocates a new
5449 * spill variable if necessary.
5452 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
5454 MonoSpillInfo **si, *info;
5457 si = &cfg->spill_info;
5459 while (i <= spillvar) {
5462 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
5464 cfg->stack_offset -= sizeof (gpointer);
5465 info->offset = cfg->stack_offset;
5469 return (*si)->offset;
5475 g_assert_not_reached ();
5480 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
5484 g_assert (bb->code);
5485 bb->last_ins->next = inst;
5486 bb->last_ins = inst;
5488 bb->last_ins = bb->code = inst;
5493 mono_destroy_compile (MonoCompile *cfg)
5495 //mono_mempool_stats (cfg->mempool);
5496 g_hash_table_destroy (cfg->bb_hash);
5498 mono_regstate_free (cfg->rs);
5499 mono_mempool_destroy (cfg->mempool);
5500 g_list_free (cfg->ldstr_list);
5502 g_free (cfg->varinfo);
5508 mono_get_lmf_addr (void)
5510 MonoJitTlsData *jit_tls;
5512 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
5513 return &jit_tls->lmf;
5515 g_assert_not_reached ();
5520 * mono_thread_abort:
5521 * @obj: exception object
5523 * abort the thread, print exception information and stack trace
5526 mono_thread_abort (MonoObject *obj)
5528 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
5536 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
5538 MonoJitTlsData *jit_tls;
5541 jit_tls = g_new0 (MonoJitTlsData, 1);
5543 TlsSetValue (mono_jit_tls_id, jit_tls);
5545 jit_tls->abort_func = mono_thread_abort;
5546 jit_tls->end_of_stack = stack_start;
5548 lmf = g_new0 (MonoLMF, 1);
5554 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
5557 mono_thread_abort_dummy (MonoObject *obj)
5559 if (mono_thread_attach_aborted_cb)
5560 mono_thread_attach_aborted_cb (obj);
5562 mono_thread_abort (obj);
5566 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
5568 MonoJitTlsData *jit_tls;
5571 jit_tls = g_new0 (MonoJitTlsData, 1);
5573 TlsSetValue (mono_jit_tls_id, jit_tls);
5575 jit_tls->abort_func = mono_thread_abort_dummy;
5576 jit_tls->end_of_stack = stack_start;
5578 lmf = g_new0 (MonoLMF, 1);
5585 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
5587 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
5591 ji->data.target = target;
5592 ji->next = cfg->patch_info;
5594 cfg->patch_info = ji;
5598 mono_remove_patch_info (MonoCompile *cfg, int ip)
5600 MonoJumpInfo **ji = &cfg->patch_info;
5603 if ((*ji)->ip.i == ip)
5606 ji = &((*ji)->next);
5611 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
5612 MonoJitICallInfo *info;
5614 switch (mono_burg_arity [tree->opcode]) {
5617 dec_foreach (tree->inst_left, cfg);
5619 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5620 MonoInst *iargs [2];
5622 iargs [0] = tree->inst_left;
5624 mono_emulate_opcode (cfg, tree, iargs, info);
5630 if (tree->opcode == OP_LMUL
5631 && (cfg->opt & MONO_OPT_INTRINS)
5632 && (tree->inst_left->opcode == CEE_CONV_I8
5633 || tree->inst_left->opcode == CEE_CONV_U8)
5634 && tree->inst_left->inst_left->type == STACK_I4
5635 && (tree->inst_right->opcode == CEE_CONV_I8
5636 || tree->inst_right->opcode == CEE_CONV_U8)
5637 && tree->inst_right->inst_left->type == STACK_I4) {
5638 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
5639 tree->inst_left = tree->inst_left->inst_left;
5640 tree->inst_right = tree->inst_right->inst_left;
5641 dec_foreach (tree, cfg);
5642 } else if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5643 MonoInst *iargs [2];
5645 iargs [0] = tree->inst_i0;
5646 iargs [1] = tree->inst_i1;
5648 mono_emulate_opcode (cfg, tree, iargs, info);
5650 dec_foreach (iargs [0], cfg);
5651 dec_foreach (iargs [1], cfg);
5654 dec_foreach (tree->inst_left, cfg);
5655 dec_foreach (tree->inst_right, cfg);
5659 g_assert_not_reached ();
5661 decompose_foreach (tree, cfg);
5665 decompose_pass (MonoCompile *cfg) {
5668 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5671 cfg->prev_ins = NULL;
5672 for (tree = cfg->cbb->code; tree; tree = tree->next) {
5673 dec_foreach (tree, cfg);
5674 cfg->prev_ins = tree;
5680 nullify_basic_block (MonoBasicBlock *bb)
5687 bb->code = bb->last_ins = NULL;
5688 bb->cil_code = NULL;
5692 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
5696 for (i = 0; i < bb->out_count; i++) {
5697 MonoBasicBlock *ob = bb->out_bb [i];
5700 if (bb->out_count > 1) {
5701 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
5705 bb->out_bb [i] = repl;
5712 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
5716 for (i = 0; i < bb->in_count; i++) {
5717 MonoBasicBlock *ib = bb->in_bb [i];
5720 if (bb->in_count > 1) {
5721 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
5725 bb->in_bb [i] = repl;
5732 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
5736 for (i = 0; i < bb->out_count; i++) {
5737 MonoBasicBlock *ob = bb->out_bb [i];
5738 for (j = 0; j < ob->in_count; j++) {
5739 if (ob->in_bb [j] == orig) {
5740 ob->in_bb [j] = repl;
5749 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
5751 bb->out_count = bbn->out_count;
5752 bb->out_bb = bbn->out_bb;
5754 replace_basic_block (bb, bbn, bb);
5758 bb->last_ins->next = bbn->code;
5759 bb->last_ins = bbn->last_ins;
5762 bb->code = bbn->code;
5763 bb->last_ins = bbn->last_ins;
5765 bb->next_bb = bbn->next_bb;
5766 nullify_basic_block (bbn);
5770 optimize_branches (MonoCompile *cfg) {
5771 int i, changed = FALSE;
5772 MonoBasicBlock *bb, *bbn;
5777 /* we skip the entry block (exit is handled specially instead ) */
5778 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
5780 /* dont touch code inside exception clauses */
5781 if (bb->region != -1)
5784 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
5785 if (cfg->verbose_level > 2)
5786 g_print ("nullify block triggered %d\n", bbn->block_num);
5788 bb->next_bb = bbn->next_bb;
5790 for (i = 0; i < bbn->out_count; i++)
5791 replace_in_block (bbn->out_bb [i], bbn, NULL);
5793 nullify_basic_block (bbn);
5797 if (bb->out_count == 1) {
5798 bbn = bb->out_bb [0];
5800 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
5801 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
5802 bb->last_ins->opcode = CEE_BR;
5803 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
5805 if (cfg->verbose_level > 2)
5806 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
5809 if (bb->region == bbn->region && bb->next_bb == bbn) {
5810 /* the block are in sequence anyway ... */
5812 /* branches to the following block can be removed */
5813 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
5814 bb->last_ins->opcode = CEE_NOP;
5816 if (cfg->verbose_level > 2)
5817 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
5820 if (bbn->in_count == 1) {
5822 if (bbn != cfg->bb_exit) {
5823 if (cfg->verbose_level > 2)
5824 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
5825 merge_basic_blocks (bb, bbn);
5829 //mono_print_bb_code (bb);
5839 /* we skip the entry block (exit is handled specially instead ) */
5840 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
5842 /* dont touch code inside exception clauses */
5843 if (bb->region != -1)
5846 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
5847 if (cfg->verbose_level > 2) {
5848 g_print ("nullify block triggered %d\n", bbn->block_num);
5850 bb->next_bb = bbn->next_bb;
5852 for (i = 0; i < bbn->out_count; i++)
5853 replace_in_block (bbn->out_bb [i], bbn, NULL);
5855 nullify_basic_block (bbn);
5861 if (bb->out_count == 1) {
5862 bbn = bb->out_bb [0];
5864 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
5865 bbn = bb->last_ins->inst_target_bb;
5866 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5867 bbn->code->inst_target_bb->region == bb->region) {
5869 if (cfg->verbose_level > 2)
5870 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
5871 bb->block_num, bbn->block_num);
5873 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
5874 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
5879 } else if (bb->out_count == 2) {
5880 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
5881 bbn = bb->last_ins->inst_true_bb;
5882 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5883 bbn->code->inst_target_bb->region == bb->region) {
5884 if (cfg->verbose_level > 2)
5885 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
5886 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
5889 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
5891 replace_in_block (bbn, bb, NULL);
5893 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
5894 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
5896 link_bblock (cfg, bb, bbn->code->inst_target_bb);
5902 bbn = bb->last_ins->inst_false_bb;
5903 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5904 bbn->code->inst_target_bb->region == bb->region) {
5905 if (cfg->verbose_level > 2)
5906 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
5907 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
5910 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
5912 replace_in_block (bbn, bb, NULL);
5914 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
5915 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
5917 link_bblock (cfg, bb, bbn->code->inst_target_bb);
5930 mono_compile_create_vars (MonoCompile *cfg)
5932 MonoMethodSignature *sig;
5933 MonoMethodHeader *header;
5936 header = ((MonoMethodNormal *)cfg->method)->header;
5938 sig = cfg->method->signature;
5940 if (!MONO_TYPE_IS_VOID (sig->ret)) {
5941 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
5942 cfg->ret->opcode = OP_RETARG;
5943 cfg->ret->inst_vtype = sig->ret;
5944 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
5948 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
5950 for (i = 0; i < sig->param_count; ++i)
5951 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
5953 cfg->locals_start = cfg->num_varinfo;
5955 for (i = 0; i < header->num_locals; ++i)
5956 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
5961 mono_print_code (MonoCompile *cfg)
5965 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5966 MonoInst *tree = bb->code;
5971 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
5973 for (; tree; tree = tree->next) {
5974 mono_print_tree (tree);
5979 bb->last_ins->next = NULL;
5984 extern const char * const mono_burg_rule_string [];
5987 emit_state (MonoCompile *cfg, MBState *state, int goal)
5990 int ern = mono_burg_rule (state, goal);
5991 const guint16 *nts = mono_burg_nts [ern];
5994 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
5998 // state->reg1 = state->reg2; /* chain rule */
6000 state->reg1 = mono_regstate_next_int (cfg->rs);
6001 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
6004 state->reg1 = mono_regstate_next_int (cfg->rs);
6005 state->reg2 = mono_regstate_next_int (cfg->rs);
6008 state->reg1 = mono_regstate_next_float (cfg->rs);
6015 mono_burg_kids (state, ern, kids);
6017 emit_state (cfg, kids [0], nts [0]);
6019 emit_state (cfg, kids [1], nts [1]);
6021 g_assert (!nts [3]);
6022 emit_state (cfg, kids [2], nts [2]);
6027 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
6028 if ((emit = mono_burg_func [ern]))
6029 emit (state, state->tree, cfg);
6032 #define DEBUG_SELECTION
6035 mini_select_instructions (MonoCompile *cfg)
6037 static int reverse_map [] = {
6038 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
6039 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
6041 static int reverse_fmap [] = {
6042 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
6043 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
6045 static int reverse_lmap [] = {
6046 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
6047 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
6052 cfg->state_pool = mono_mempool_new ();
6053 cfg->rs = mono_regstate_new ();
6055 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6056 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode) &&
6057 bb->next_bb != bb->last_ins->inst_false_bb) {
6059 if (bb->next_bb == bb->last_ins->inst_true_bb) {
6060 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
6061 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
6062 bb->last_ins->inst_false_bb = tmp;
6064 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
6065 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
6066 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
6067 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
6068 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
6069 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
6072 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
6073 inst->opcode = CEE_BR;
6074 inst->inst_target_bb = bb->last_ins->inst_false_bb;
6075 mono_bblock_add_inst (bb, inst);
6080 #ifdef DEBUG_SELECTION
6081 if (cfg->verbose_level >= 4) {
6082 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6083 MonoInst *tree = bb->code;
6084 g_print ("DUMP BLOCK %d:\n", bb->block_num);
6087 for (; tree; tree = tree->next) {
6088 mono_print_tree (tree);
6095 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6096 MonoInst *tree = bb->code, *next;
6102 bb->last_ins = NULL;
6105 mono_regstate_reset (cfg->rs);
6107 #ifdef DEBUG_SELECTION
6108 if (cfg->verbose_level >= 3)
6109 g_print ("LABEL BLOCK %d:\n", bb->block_num);
6111 for (; tree; tree = next) {
6113 #ifdef DEBUG_SELECTION
6114 if (cfg->verbose_level >= 3) {
6115 mono_print_tree (tree);
6120 if (!(mbstate = mono_burg_label (tree, cfg))) {
6121 g_warning ("unabled to label tree %p", tree);
6122 mono_print_tree (tree);
6124 g_assert_not_reached ();
6126 emit_state (cfg, mbstate, MB_NTERM_stmt);
6128 bb->max_ireg = cfg->rs->next_vireg;
6129 bb->max_freg = cfg->rs->next_vfreg;
6132 bb->last_ins->next = NULL;
6134 mono_mempool_empty (cfg->state_pool);
6136 mono_mempool_destroy (cfg->state_pool);
6140 mono_codegen (MonoCompile *cfg)
6142 MonoJumpInfo *patch_info;
6144 int i, max_epilog_size;
6147 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6148 cfg->spill_count = 0;
6149 /* we reuse dfn here */
6150 /* bb->dfn = bb_count++; */
6151 mono_arch_local_regalloc (cfg, bb);
6154 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
6155 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
6157 code = mono_arch_emit_prolog (cfg);
6159 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6160 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
6162 cfg->code_len = code - cfg->native_code;
6163 cfg->prolog_end = cfg->code_len;
6165 mono_debug_open_method (cfg);
6167 /* emit code all basic blocks */
6168 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6169 bb->native_offset = cfg->code_len;
6170 mono_arch_output_basic_block (cfg, bb);
6172 cfg->bb_exit->native_offset = cfg->code_len;
6174 code = cfg->native_code + cfg->code_len;
6176 max_epilog_size = mono_arch_max_epilog_size (cfg);
6178 /* we always allocate code in cfg->domain->code_mp to increase locality */
6179 cfg->code_size = cfg->code_len + max_epilog_size;
6180 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
6181 code = mono_mempool_alloc (cfg->domain->code_mp, cfg->code_size);
6182 memcpy (code, cfg->native_code, cfg->code_len);
6183 g_free (cfg->native_code);
6184 cfg->native_code = code;
6185 code = cfg->native_code + cfg->code_len;
6187 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
6189 cfg->epilog_begin = cfg->code_len;
6191 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6192 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
6194 cfg->code_len = code - cfg->native_code;
6196 mono_arch_emit_epilog (cfg);
6198 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
6199 switch (patch_info->type) {
6200 case MONO_PATCH_INFO_ABS: {
6201 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
6203 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
6204 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
6205 patch_info->data.name = info->name;
6209 case MONO_PATCH_INFO_SWITCH: {
6210 gpointer *table = g_new (gpointer, patch_info->table_size);
6211 patch_info->ip.i = patch_info->ip.label->inst_c0;
6212 for (i = 0; i < patch_info->table_size; i++) {
6213 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
6215 patch_info->data.target = table;
6224 if (cfg->verbose_level > 1)
6225 g_print ("Method %s::%s emmitted at %p to %p\n", cfg->method->klass->name,
6226 cfg->method->name, cfg->native_code, cfg->native_code + cfg->code_len);
6228 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info);
6230 mono_debug_close_method (cfg);
6234 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
6239 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
6240 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
6242 if (cp->opcode == OP_ICONST) {
6243 if (cfg->opt & MONO_OPT_CONSPROP) {
6244 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
6248 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
6249 if (cfg->opt & MONO_OPT_COPYPROP) {
6250 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
6256 arity = mono_burg_arity [tree->opcode];
6259 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
6260 if (cfg->opt & MONO_OPT_CFOLD)
6261 mono_constant_fold_inst (tree, NULL);
6263 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
6264 if (cfg->opt & MONO_OPT_CFOLD)
6265 mono_constant_fold_inst (tree, NULL);
6267 mono_constant_fold_inst (tree, NULL);
6273 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
6277 switch (tree->opcode) {
6287 if (tree->ssa_op == MONO_SSA_NOP) {
6288 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6305 case OP_VOIDCALL_REG:
6306 case OP_VOIDCALLVIRT:
6308 MonoCallInst *call = (MonoCallInst *)tree;
6309 MonoMethodSignature *sig = call->signature;
6310 int i, byref = FALSE;
6312 for (i = 0; i < sig->param_count; i++) {
6313 if (sig->params [i]->byref) {
6320 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6328 arity = mono_burg_arity [tree->opcode];
6334 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6337 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6338 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
6341 g_assert_not_reached ();
6346 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
6348 MonoInst *tree = bb->code;
6354 for (; tree; tree = tree->next) {
6356 mono_cprop_copy_values (cfg, tree, acp);
6358 mono_cprop_invalidate_values (tree, acp, acp_size);
6360 if (tree->ssa_op == MONO_SSA_STORE &&
6361 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
6362 MonoInst *i1 = tree->inst_i1;
6364 acp [tree->inst_i0->inst_c0] = NULL;
6366 for (i = 0; i < acp_size; i++) {
6367 if (acp [i] && acp [i]->opcode != OP_ICONST &&
6368 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
6373 if (i1->opcode == OP_ICONST) {
6374 acp [tree->inst_i0->inst_c0] = i1;
6375 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
6377 if (i1->ssa_op == MONO_SSA_LOAD &&
6378 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
6379 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
6380 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
6381 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
6386 if (tree->opcode == CEE_BEQ) {
6387 g_assert (tree->inst_i0->opcode == OP_COMPARE);
6388 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
6389 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
6391 tree->opcode = CEE_BR;
6392 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
6393 tree->inst_target_bb = tree->inst_true_bb;
6395 tree->inst_target_bb = tree->inst_false_bb;
6404 mono_local_cprop (MonoCompile *cfg)
6409 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
6411 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6412 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
6413 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
6418 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int parts)
6420 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
6421 guint8 *ip = (guint8 *)header->code;
6424 int dfn = 0, i, code_size_ratio;
6426 mono_jit_stats.methods_compiled++;
6427 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
6428 mono_profiler_method_jit (method);
6430 cfg = g_new0 (MonoCompile, 1);
6431 cfg->method = method;
6432 cfg->mempool = mono_mempool_new ();
6434 cfg->prof_options = mono_profiler_get_events ();
6435 cfg->bb_hash = g_hash_table_new (g_direct_hash, NULL);
6436 cfg->domain = domain;
6437 cfg->verbose_level = mini_verbose;
6438 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
6439 ((MonoMethodNormal *)method)->header->max_stack);
6442 * create MonoInst* which represents arguments and local variables
6444 mono_compile_create_vars (cfg);
6446 if (cfg->verbose_level > 2)
6447 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
6449 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
6450 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
6451 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
6452 mono_destroy_compile (cfg);
6456 mono_jit_stats.basic_blocks += cfg->num_bblocks;
6457 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
6459 if (cfg->num_varinfo > 2000) {
6461 * we disable some optimizations if there are too many variables
6462 * because JIT time may become too expensive. The actual number needs
6463 * to be tweaked and eventually the non-linear algorithms should be fixed.
6465 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
6466 cfg->disable_ssa = TRUE;
6468 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
6470 /* Depth-first ordering on basic blocks */
6471 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
6473 if (cfg->opt & MONO_OPT_BRANCH)
6474 optimize_branches (cfg);
6476 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
6477 if (cfg->num_bblocks != dfn + 1) {
6480 cfg->num_bblocks = dfn + 1;
6482 if (!header->clauses) {
6483 /* remove unreachable code, because the code in them may be
6484 * inconsistent (access to dead variables for example) */
6485 for (bb = cfg->bb_entry; bb;) {
6486 MonoBasicBlock *bbn = bb->next_bb;
6488 if (bbn && bbn->region == -1 && !bbn->dfn) {
6489 if (cfg->verbose_level > 1)
6490 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
6491 bb->next_bb = bbn->next_bb;
6492 nullify_basic_block (bbn);
6500 if (cfg->opt & MONO_OPT_LOOP) {
6501 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
6502 mono_compute_natural_loops (cfg);
6506 /* after method_to_ir */
6510 //#define DEBUGSSA "logic_run"
6511 #define DEBUGSSA_CLASS "Tests"
6515 if (!header->num_clauses && !cfg->disable_ssa) {
6516 mono_local_cprop (cfg);
6517 mono_ssa_compute (cfg);
6521 /* fixme: add all optimizations which requires SSA */
6522 if (cfg->opt & (MONO_OPT_DEADCE)) {
6523 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
6524 mono_local_cprop (cfg);
6525 mono_ssa_compute (cfg);
6527 if (cfg->verbose_level >= 2) {
6534 /* after SSA translation */
6538 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
6539 if (cfg->comp_done & MONO_COMP_SSA) {
6540 mono_ssa_cprop (cfg);
6542 mono_local_cprop (cfg);
6546 if (cfg->comp_done & MONO_COMP_SSA) {
6547 mono_ssa_deadce (cfg);
6549 //mono_ssa_strength_reduction (cfg);
6551 mono_ssa_remove (cfg);
6553 if (cfg->opt & MONO_OPT_BRANCH)
6554 optimize_branches (cfg);
6557 /* after SSA removal */
6561 decompose_pass (cfg);
6563 /* FIXME: disabled with exception clauses: bug #42136 */
6564 if ((!header->num_clauses) && (cfg->opt & MONO_OPT_LINEARS)) {
6567 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
6568 cfg->comp_done &= ~MONO_COMP_LIVENESS;
6569 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
6570 mono_analyze_liveness (cfg);
6572 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
6573 regs = mono_arch_get_global_int_regs (cfg);
6574 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
6578 //mono_print_code (cfg);
6582 /* variables are allocated after decompose, since decompose could create temps */
6583 mono_arch_allocate_vars (cfg);
6585 if (cfg->opt & MONO_OPT_CFOLD)
6586 mono_constant_fold (cfg);
6588 mini_select_instructions (cfg);
6591 if (cfg->verbose_level >= 2) {
6592 char *id = mono_method_full_name (cfg->method, FALSE);
6593 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
6597 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
6599 jinfo = g_new0 (MonoJitInfo, 1);
6600 jinfo->method = method;
6601 jinfo->code_start = cfg->native_code;
6602 jinfo->code_size = cfg->code_len;
6603 jinfo->used_regs = cfg->used_int_regs;
6605 if (header->num_clauses) {
6608 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
6609 jinfo->num_clauses = header->num_clauses;
6610 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
6611 sizeof (MonoJitExceptionInfo) * header->num_clauses);
6613 for (i = 0; i < header->num_clauses; i++) {
6614 MonoExceptionClause *ec = &header->clauses [i];
6615 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
6616 MonoBasicBlock *tblock;
6618 ei->flags = ec->flags;
6620 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
6621 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
6623 ei->data.filter = cfg->native_code + tblock->native_offset;
6625 ei->data.token = ec->token_or_filter;
6628 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
6630 ei->try_start = cfg->native_code + tblock->native_offset;
6631 g_assert (tblock->native_offset);
6632 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
6634 ei->try_end = cfg->native_code + tblock->native_offset;
6635 g_assert (tblock->native_offset);
6636 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
6638 ei->handler_start = cfg->native_code + tblock->native_offset;
6642 mono_jit_info_table_add (cfg->domain, jinfo);
6644 /* collect statistics */
6645 mono_jit_stats.allocated_code_size += cfg->code_len;
6646 code_size_ratio = cfg->code_len;
6647 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
6648 mono_jit_stats.biggest_method_size = code_size_ratio;
6649 mono_jit_stats.biggest_method = method;
6651 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
6652 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
6653 mono_jit_stats.max_code_size_ratio = code_size_ratio;
6654 mono_jit_stats.max_ratio_method = method;
6656 mono_jit_stats.native_code_size += cfg->code_len;
6658 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
6659 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
6665 mono_jit_compile_method (MonoMethod *method)
6667 /* FIXME: later copy the code from mono */
6668 MonoDomain *target_domain, *domain = mono_domain_get ();
6670 GHashTable *jit_code_hash;
6673 if (default_opt & MONO_OPT_SHARED)
6674 target_domain = mono_root_domain;
6676 target_domain = domain;
6678 jit_code_hash = target_domain->jit_code_hash;
6680 if ((code = g_hash_table_lookup (jit_code_hash, method))) {
6681 mono_jit_stats.methods_lookups++;
6685 #ifdef MONO_USE_AOT_COMPILER
6686 if (!mono_compile_aot) {
6687 mono_class_init (method->klass);
6688 if ((code = mono_aot_get_method (method))) {
6689 g_hash_table_insert (jit_code_hash, method, code);
6695 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
6696 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
6697 if (!method->info) {
6700 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
6701 mono_lookup_pinvoke_call (method);
6702 #ifdef MONO_USE_EXC_TABLES
6703 if (mono_method_blittable (method)) {
6704 method->info = method->addr;
6707 nm = mono_marshal_get_native_wrapper (method);
6708 method->info = mono_compile_method (nm);
6710 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
6711 //mono_debug_add_wrapper (method, nm);
6712 #ifdef MONO_USE_EXC_TABLES
6716 return method->info;
6717 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
6718 const char *name = method->name;
6721 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
6722 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
6723 /* FIXME: uhm, we need a wrapper to handle exceptions? */
6724 return (gpointer)mono_delegate_ctor;
6725 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
6726 nm = mono_marshal_get_delegate_invoke (method);
6727 return mono_jit_compile_method (nm);
6728 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
6729 nm = mono_marshal_get_delegate_begin_invoke (method);
6730 return mono_jit_compile_method (nm);
6731 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
6732 nm = mono_marshal_get_delegate_end_invoke (method);
6733 return mono_jit_compile_method (nm);
6739 cfg = mini_method_compile (method, default_opt, target_domain, 0);
6740 code = cfg->native_code;
6741 mono_destroy_compile (cfg);
6743 g_hash_table_insert (jit_code_hash, method, code);
6745 if (target_domain->jump_target_hash) {
6746 MonoJumpInfo patch_info;
6748 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
6750 patch_info.next = NULL;
6751 patch_info.ip.i = 0;
6752 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
6753 patch_info.data.method = method;
6754 g_hash_table_remove (target_domain->jump_target_hash, method);
6756 for (tmp = list; tmp; tmp = tmp->next)
6757 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info);
6758 g_slist_free (list);
6760 /* make sure runtime_init is called */
6761 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
6767 * mono_jit_runtime_invoke:
6768 * @method: the method to invoke
6769 * @obj: this pointer
6770 * @params: array of parameter values.
6771 * @exc: used to catch exceptions objects
6774 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
6777 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
6778 invoke = mono_marshal_get_runtime_invoke (method);
6779 runtime_invoke = mono_jit_compile_method (invoke);
6780 return runtime_invoke (obj, params, exc);
6783 #ifdef PLATFORM_WIN32
6784 #define GET_CONTEXT \
6785 struct sigcontext *ctx = (struct sigcontext*)_dummy;
6787 #define GET_CONTEXT \
6788 void **_p = (void **)&_dummy; \
6789 struct sigcontext *ctx = (struct sigcontext *)++_p;
6793 sigfpe_signal_handler (int _dummy)
6798 exc = mono_get_exception_divide_by_zero ();
6800 mono_arch_handle_exception (ctx, exc, FALSE);
6804 sigill_signal_handler (int _dummy)
6808 exc = mono_get_exception_execution_engine ("SIGILL");
6810 mono_arch_handle_exception (ctx, exc, FALSE);
6814 sigsegv_signal_handler (int _dummy)
6819 exc = mono_get_exception_null_reference ();
6821 mono_arch_handle_exception (ctx, exc, FALSE);
6825 sigusr1_signal_handler (int _dummy)
6830 thread = mono_thread_current ();
6832 g_assert (thread->abort_exc);
6834 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
6838 sigquit_signal_handler (int _dummy)
6843 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
6845 mono_arch_handle_exception (ctx, exc, FALSE);
6850 mono_runtime_install_handlers (void)
6852 #ifndef PLATFORM_WIN32
6853 struct sigaction sa;
6856 #ifdef PLATFORM_WIN32
6858 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
6859 win32_seh_set_handler(SIGILL, sigill_signal_handler);
6860 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
6861 #else /* !PLATFORM_WIN32 */
6863 /* libpthreads has its own implementation of sigaction(),
6864 * but it seems to work well with our current exception
6865 * handlers. If not we must call syscall directly instead
6869 sa.sa_handler = sigfpe_signal_handler;
6870 sigemptyset (&sa.sa_mask);
6872 //g_assert (syscall (SYS_sigaction, SIGFPE, &sa, NULL) != -1);
6873 g_assert (sigaction (SIGFPE, &sa, NULL) != -1);
6876 sa.sa_handler = sigquit_signal_handler;
6877 sigemptyset (&sa.sa_mask);
6879 g_assert (sigaction (SIGQUIT, &sa, NULL) != -1);
6882 sa.sa_handler = sigill_signal_handler;
6883 sigemptyset (&sa.sa_mask);
6885 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
6886 g_assert (sigaction (SIGILL, &sa, NULL) != -1);
6888 /* catch thread abort signal */
6889 sa.sa_handler = sigusr1_signal_handler;
6890 sigemptyset (&sa.sa_mask);
6892 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
6893 //g_assert (sigaction (mono_thread_get_abort_signal (), &sa, NULL) != -1);
6897 sa.sa_handler = sigsegv_signal_handler;
6898 sigemptyset (&sa.sa_mask);
6900 //g_assert (syscall (SYS_sigaction, SIGSEGV, &sa, NULL) != -1);
6901 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
6903 #endif /* PLATFORM_WIN32 */
6906 /* mono_jit_create_remoting_trampoline:
6907 * @method: pointer to the method info
6909 * Creates a trampoline which calls the remoting functions. This
6910 * is used in the vtable of transparent proxies.
6912 * Returns: a pointer to the newly created code
6915 mono_jit_create_remoting_trampoline (MonoMethod *method)
6918 guint8 *addr = NULL;
6920 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
6921 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
6922 nm = mono_marshal_get_remoting_invoke (method);
6923 addr = mono_compile_method (nm);
6925 addr = mono_compile_method (method);
6930 static CRITICAL_SECTION ms;
6933 mini_init (const char *filename)
6937 mono_arch_cpu_init ();
6939 metadata_section = &ms;
6940 InitializeCriticalSection (metadata_section);
6942 mono_jit_tls_id = TlsAlloc ();
6943 mono_thread_start_cb (GetCurrentThreadId (), (gpointer)-1, NULL);
6947 mono_runtime_install_handlers ();
6949 mono_install_compile_method (mono_jit_compile_method);
6950 mono_install_trampoline (mono_arch_create_jit_trampoline);
6951 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
6952 mono_install_runtime_invoke (mono_jit_runtime_invoke);
6953 mono_install_handler (mono_arch_get_throw_exception ());
6954 mono_install_stack_walk (mono_jit_walk_stack);
6955 mono_install_get_config_dir ();
6957 domain = mono_init (filename);
6960 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
6961 ves_icall_get_frame_info);
6962 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
6963 ves_icall_get_trace);
6964 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
6965 mono_runtime_install_handlers);
6968 create_helper_signature ();
6970 mono_arch_register_lowlevel_calls ();
6971 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
6972 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
6974 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
6975 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
6977 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
6978 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
6979 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
6981 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
6982 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
6983 helper_sig_void_ptr, TRUE);
6986 * NOTE, NOTE, NOTE, NOTE:
6987 * when adding emulation for some opcodes, remember to also add a dummy
6988 * rule to the burg files, because we need the arity information to be correct.
6990 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
6991 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
6992 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
6993 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
6994 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
6995 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
6996 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
6998 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
6999 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
7000 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
7002 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
7003 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
7004 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, TRUE);
7005 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, TRUE);
7007 #if SIZEOF_VOID_P == 4
7008 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
7010 #warning "fixme: add opcode emulation"
7013 /* other jit icalls */
7014 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
7015 helper_sig_ptr_ptr_ptr, FALSE);
7016 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
7017 mono_register_jit_icall (mono_threads_get_static_data, "mono_threads_get_static_data", helper_sig_ptr_int, FALSE);
7018 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
7019 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
7020 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
7021 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
7022 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
7023 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
7024 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
7025 mono_register_jit_icall (mono_object_new_fast, "mono_object_new_fast", helper_sig_object_new_specific, FALSE);
7026 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
7027 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
7028 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
7029 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
7030 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
7031 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
7032 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
7033 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
7034 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
7035 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
7036 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
7037 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
7038 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
7039 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
7040 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
7041 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
7042 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
7043 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
7044 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
7045 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
7046 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
7048 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
7049 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
7051 //mono_thread_attach (domain);
7055 MonoJitStats mono_jit_stats = {0};
7058 print_jit_stats (void)
7060 if (mono_jit_stats.enabled) {
7061 g_print ("Mono Jit statistics\n");
7062 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
7063 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
7064 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
7065 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
7066 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
7067 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
7068 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
7069 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
7070 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
7071 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
7072 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
7073 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
7074 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
7075 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
7076 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
7077 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
7078 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
7079 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
7081 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
7082 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
7083 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
7084 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
7085 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
7090 mini_cleanup (MonoDomain *domain)
7093 * mono_runtime_cleanup() and mono_domain_finalize () need to
7094 * be called early since they need the execution engine still
7095 * fully working (mono_domain_finalize may invoke managed finalizers
7096 * and mono_runtime_cleanup will wait for other threads to finish).
7098 mono_domain_finalize (domain);
7100 mono_runtime_cleanup (domain);
7102 mono_profiler_shutdown ();
7104 mono_debug_cleanup ();
7106 #ifdef PLATFORM_WIN32
7107 win32_seh_cleanup();
7110 mono_domain_unload (domain, TRUE);
7113 DeleteCriticalSection (metadata_section);
7117 mono_set_defaults (int verbose_level, guint32 opts)
7119 mini_verbose = verbose_level;