2 * mini.c: The new Mono code generator.
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2002 Ximian, Inc.
15 #include <mono/metadata/assembly.h>
16 #include <mono/metadata/loader.h>
17 #include <mono/metadata/cil-coff.h>
18 #include <mono/metadata/tabledefs.h>
19 #include <mono/metadata/class.h>
20 #include <mono/metadata/object.h>
21 #include <mono/metadata/exception.h>
22 #include <mono/metadata/opcodes.h>
23 #include <mono/metadata/mono-endian.h>
24 #include <mono/metadata/tokentype.h>
25 #include <mono/metadata/tabledefs.h>
26 #include <mono/metadata/threads.h>
27 #include <mono/metadata/marshal.h>
28 #include <mono/metadata/socket-io.h>
29 #include <mono/metadata/appdomain.h>
30 #include <mono/metadata/debug-helpers.h>
31 #include <mono/io-layer/io-layer.h>
32 #include "mono/metadata/profiler.h"
33 #include <mono/metadata/profiler-private.h>
34 #include <mono/metadata/mono-config.h>
35 #include <mono/metadata/environment.h>
36 #include <mono/metadata/mono-debug.h>
37 #include <mono/metadata/mono-debug-debugger.h>
44 #include "jit-icalls.c"
46 #define MONO_CHECK_THIS(ins) (cfg->method->signature->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
48 gboolean mono_arch_handle_exception (struct sigcontext *ctx, gpointer obj, gboolean test_only);
49 static gpointer mono_jit_compile_method (MonoMethod *method);
51 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
52 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
54 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
55 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
56 guint inline_offset, gboolean is_virtual_call);
58 extern guint8 mono_burg_arity [];
59 /* helper methods signature */
60 static MonoMethodSignature *helper_sig_long_long_long = NULL;
61 static MonoMethodSignature *helper_sig_long_long_int = NULL;
62 static MonoMethodSignature *helper_sig_newarr = NULL;
63 static MonoMethodSignature *helper_sig_newarr_specific = NULL;
64 static MonoMethodSignature *helper_sig_ldstr = NULL;
65 static MonoMethodSignature *helper_sig_domain_get = NULL;
66 static MonoMethodSignature *helper_sig_object_new = NULL;
67 static MonoMethodSignature *helper_sig_object_new_specific = NULL;
68 static MonoMethodSignature *helper_sig_compile = NULL;
69 static MonoMethodSignature *helper_sig_compile_virt = NULL;
70 static MonoMethodSignature *helper_sig_obj_ptr = NULL;
71 static MonoMethodSignature *helper_sig_ptr_void = NULL;
72 static MonoMethodSignature *helper_sig_void_ptr = NULL;
73 static MonoMethodSignature *helper_sig_void_obj = NULL;
74 static MonoMethodSignature *helper_sig_void_ptr_ptr = NULL;
75 static MonoMethodSignature *helper_sig_void_ptr_ptr_ptr = NULL;
76 static MonoMethodSignature *helper_sig_ptr_ptr_ptr = NULL;
77 static MonoMethodSignature *helper_sig_ptr_obj = NULL;
78 static MonoMethodSignature *helper_sig_ptr_int = NULL;
79 static MonoMethodSignature *helper_sig_initobj = NULL;
80 static MonoMethodSignature *helper_sig_memcpy = NULL;
81 static MonoMethodSignature *helper_sig_memset = NULL;
82 static MonoMethodSignature *helper_sig_ulong_double = NULL;
83 static MonoMethodSignature *helper_sig_long_double = NULL;
84 static MonoMethodSignature *helper_sig_uint_double = NULL;
85 static MonoMethodSignature *helper_sig_int_double = NULL;
86 static MonoMethodSignature *helper_sig_stelem_ref = NULL;
88 static guint32 default_opt = MONO_OPT_PEEPHOLE;
90 guint32 mono_jit_tls_id = 0;
91 gboolean mono_jit_trace_calls = FALSE;
92 gboolean mono_break_on_exc = FALSE;
93 gboolean mono_compile_aot = FALSE;
94 gboolean mono_trace_coverage = FALSE;
95 gboolean mono_jit_profile = FALSE;
96 MonoDebugFormat mono_debug_format = MONO_DEBUG_FORMAT_NONE;
98 CRITICAL_SECTION *metadata_section = NULL;
100 static int mini_verbose = 0;
102 #ifdef MONO_USE_EXC_TABLES
104 mono_type_blittable (MonoType *type)
123 case MONO_TYPE_OBJECT:
125 case MONO_TYPE_VALUETYPE:
126 case MONO_TYPE_CLASS:
127 return type->data.klass->blittable;
137 mono_method_blittable (MonoMethod *method)
139 MonoMethodSignature *sig;
145 if (!mono_arch_has_unwind_info (method->addr)) {
149 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
152 sig = method->signature;
154 if (!mono_type_blittable (sig->ret))
157 for (i = 0; i < sig->param_count; i++)
158 if (!mono_type_blittable (sig->params [i]))
167 print_method_from_ip (void *ip)
172 ji = mono_jit_info_table_find (mono_domain_get (), ip);
174 g_print ("No method at %p\n", ip);
177 method = mono_method_full_name (ji->method, TRUE);
178 g_print ("IP at offset 0x%x of method %s (%p %p)\n", (char*)ip - (char*)ji->code_start, method, ji->code_start, (char*)ji->code_start + ji->code_size);
183 #define MONO_INIT_VARINFO(vi,id) do { \
184 (vi)->range.first_use.pos.bid = 0xffff; \
190 * Basic blocks have two numeric identifiers:
191 * dfn: Depth First Number
192 * block_num: unique ID assigned at bblock creation
194 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
195 #define ADD_BBLOCK(cfg,bbhash,b) do { \
196 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
197 (b)->block_num = cfg->num_bblocks++; \
198 (b)->real_offset = real_offset; \
201 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
202 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
204 if ((ip) >= end || (ip) < header->code) goto unverified; \
205 (tblock) = NEW_BBLOCK (cfg); \
206 (tblock)->cil_code = (ip); \
207 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
209 (tblock)->real_offset = real_offset; \
212 #define CHECK_BBLOCK(target,ip,tblock) do { \
213 if ((target) < (ip) && !(tblock)->code) { \
214 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
215 if (cfg->verbose_level > 2) g_print ("queued block %d for check at IL%04x from IL%04x\n", (tblock)->block_num, (target) - header->code, (ip) - header->code); \
219 #define NEW_ICONST(cfg,dest,val) do { \
220 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
221 (dest)->opcode = OP_ICONST; \
222 (dest)->inst_c0 = (val); \
223 (dest)->type = STACK_I4; \
226 /* FIXME: have a different definition of NEW_PCONST for 64 bit systems */
227 #define NEW_PCONST(cfg,dest,val) do { \
228 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
229 (dest)->opcode = OP_ICONST; \
230 (dest)->inst_p0 = (val); \
231 (dest)->type = STACK_PTR; \
234 #define NEW_CLASSCONST(cfg,dest,val) do { \
235 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
236 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
237 (dest)->inst_p0 = (val); \
238 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_CLASS; \
239 (dest)->type = STACK_PTR; \
242 #define NEW_IMAGECONST(cfg,dest,val) do { \
243 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
244 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
245 (dest)->inst_p0 = (val); \
246 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_IMAGE; \
247 (dest)->type = STACK_PTR; \
250 #define NEW_FIELDCONST(cfg,dest,field) do { \
251 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
252 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
253 (dest)->inst_p0 = (field); \
254 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_FIELD; \
255 (dest)->type = STACK_PTR; \
258 #define NEW_METHODCONST(cfg,dest,val) do { \
259 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
260 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
261 (dest)->inst_p0 = (val); \
262 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_METHODCONST; \
263 (dest)->type = STACK_PTR; \
266 #define NEW_DOMAINCONST(cfg,dest) do { \
267 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) { \
268 NEW_TEMPLOAD (cfg, dest, mono_get_domainvar (cfg)->inst_c0); \
270 NEW_PCONST (cfg, dest, (cfg)->domain); \
274 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
276 #define NEW_ARGLOAD(cfg,dest,num) do { \
277 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
278 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
279 (dest)->ssa_op = MONO_SSA_LOAD; \
280 (dest)->inst_i0 = arg_array [(num)]; \
281 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
282 type_to_eval_stack_type (param_types [(num)], (dest)); \
283 (dest)->klass = (dest)->inst_i0->klass; \
286 #define NEW_LOCLOAD(cfg,dest,num) do { \
287 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
288 (dest)->ssa_op = MONO_SSA_LOAD; \
289 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
290 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
291 type_to_eval_stack_type (header->locals [(num)], (dest)); \
292 (dest)->klass = (dest)->inst_i0->klass; \
295 #define NEW_LOCLOADA(cfg,dest,num) do { \
296 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
297 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
298 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
299 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
300 (dest)->opcode = OP_LDADDR; \
301 (dest)->type = STACK_MP; \
302 (dest)->klass = (dest)->inst_i0->klass; \
303 (cfg)->disable_ssa = TRUE; \
306 #define NEW_RETLOADA(cfg,dest) do { \
307 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
308 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
309 (dest)->inst_i0 = (cfg)->ret; \
310 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
311 (dest)->opcode = CEE_LDIND_I; \
312 (dest)->type = STACK_MP; \
313 (dest)->klass = (dest)->inst_i0->klass; \
314 (cfg)->disable_ssa = TRUE; \
317 #define NEW_ARGLOADA(cfg,dest,num) do { \
318 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
319 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
320 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
321 (dest)->inst_i0 = arg_array [(num)]; \
322 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
323 (dest)->opcode = OP_LDADDR; \
324 (dest)->type = STACK_MP; \
325 (dest)->klass = (dest)->inst_i0->klass; \
326 (cfg)->disable_ssa = TRUE; \
329 #define NEW_TEMPLOAD(cfg,dest,num) do { \
330 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
331 (dest)->ssa_op = MONO_SSA_LOAD; \
332 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
333 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
334 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
335 (dest)->klass = (dest)->inst_i0->klass; \
338 #define NEW_TEMPLOADA(cfg,dest,num) do { \
339 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
340 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
341 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
342 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
343 (dest)->opcode = OP_LDADDR; \
344 (dest)->type = STACK_MP; \
345 (dest)->klass = (dest)->inst_i0->klass; \
346 (cfg)->disable_ssa = TRUE; \
350 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
351 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
352 (dest)->inst_left = addr; \
353 (dest)->opcode = mono_type_to_ldind (vtype); \
354 type_to_eval_stack_type (vtype, (dest)); \
355 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
358 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
359 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
360 (dest)->inst_i0 = addr; \
361 (dest)->opcode = mono_type_to_stind (vtype); \
362 (dest)->inst_i1 = (value); \
363 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
366 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
367 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
368 (dest)->ssa_op = MONO_SSA_STORE; \
369 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
370 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
371 (dest)->inst_i1 = (inst); \
372 (dest)->klass = (dest)->inst_i0->klass; \
375 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
376 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
377 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
378 (dest)->ssa_op = MONO_SSA_STORE; \
379 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
380 (dest)->inst_i1 = (inst); \
381 (dest)->klass = (dest)->inst_i0->klass; \
384 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
385 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
386 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
387 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
388 (dest)->ssa_op = MONO_SSA_STORE; \
389 (dest)->inst_i0 = arg_array [(num)]; \
390 (dest)->inst_i1 = (inst); \
391 (dest)->klass = (dest)->inst_i0->klass; \
394 #define ADD_BINOP(op) do { \
395 MONO_INST_NEW (cfg, ins, (op)); \
396 ins->cil_code = ip; \
398 ins->inst_i0 = sp [0]; \
399 ins->inst_i1 = sp [1]; \
401 type_from_op (ins); \
405 #define ADD_UNOP(op) do { \
406 MONO_INST_NEW (cfg, ins, (op)); \
407 ins->cil_code = ip; \
409 ins->inst_i0 = sp [0]; \
411 type_from_op (ins); \
415 #define ADD_BINCOND(next_block) do { \
417 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
419 cmp->inst_i0 = sp [0]; \
420 cmp->inst_i1 = sp [1]; \
421 cmp->cil_code = ins->cil_code; \
422 type_from_op (cmp); \
424 ins->inst_i0 = cmp; \
425 MONO_ADD_INS (bblock, ins); \
426 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
427 GET_BBLOCK (cfg, bbhash, tblock, target); \
428 link_bblock (cfg, bblock, tblock); \
429 ins->inst_true_bb = tblock; \
430 CHECK_BBLOCK (target, ip, tblock); \
431 if ((next_block)) { \
432 link_bblock (cfg, bblock, (next_block)); \
433 ins->inst_false_bb = (next_block); \
434 start_new_bblock = 1; \
436 GET_BBLOCK (cfg, bbhash, tblock, ip); \
437 link_bblock (cfg, bblock, tblock); \
438 ins->inst_false_bb = tblock; \
439 start_new_bblock = 2; \
443 /* FIXME: handle float, long ... */
444 #define ADD_UNCOND(istrue) do { \
446 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
448 cmp->inst_i0 = sp [0]; \
449 switch (cmp->inst_i0->type) { \
451 cmp->inst_i1 = zero_int64; break; \
453 cmp->inst_i1 = zero_r8; break; \
456 cmp->inst_i1 = zero_ptr; break; \
458 cmp->inst_i1 = zero_obj; break; \
460 cmp->inst_i1 = zero_int32; \
462 cmp->cil_code = ins->cil_code; \
463 type_from_op (cmp); \
465 ins->inst_i0 = cmp; \
466 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
467 MONO_ADD_INS (bblock, ins); \
468 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
469 GET_BBLOCK (cfg, bbhash, tblock, target); \
470 link_bblock (cfg, bblock, tblock); \
471 ins->inst_true_bb = tblock; \
472 CHECK_BBLOCK (target, ip, tblock); \
473 GET_BBLOCK (cfg, bbhash, tblock, ip); \
474 link_bblock (cfg, bblock, tblock); \
475 ins->inst_false_bb = tblock; \
476 start_new_bblock = 2; \
479 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
480 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
481 (dest)->opcode = CEE_LDELEMA; \
482 (dest)->inst_left = (sp) [0]; \
483 (dest)->inst_right = (sp) [1]; \
484 (dest)->type = STACK_MP; \
485 (dest)->klass = (k); \
488 #define NEW_GROUP(cfg,dest,el1,el2) do { \
489 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
490 (dest)->opcode = OP_GROUP; \
491 (dest)->inst_left = (el1); \
492 (dest)->inst_right = (el2); \
495 static GHashTable *coverage_hash = NULL;
498 mono_allocate_coverage_info (MonoMethod *method, int size)
500 MonoCoverageInfo *res;
503 coverage_hash = g_hash_table_new (NULL, NULL);
505 res = g_malloc0 (sizeof (MonoCoverageInfo) + sizeof (int) * size * 2);
509 g_hash_table_insert (coverage_hash, method, res);
515 mono_get_coverage_info (MonoMethod *method)
520 return g_hash_table_lookup (coverage_hash, method);
525 compare_bblock (gconstpointer a, gconstpointer b)
527 const MonoBasicBlock *b1 = a;
528 const MonoBasicBlock *b2 = b;
530 return b2->cil_code - b1->cil_code;
535 * link_bblock: Links two basic blocks
537 * links two basic blocks in the control flow graph, the 'from'
538 * argument is the starting block and the 'to' argument is the block
539 * the control flow ends to after 'from'.
542 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
544 MonoBasicBlock **newa;
548 if (from->cil_code) {
550 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
552 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
555 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
557 g_print ("edge from entry to exit\n");
561 for (i = 0; i < from->out_count; ++i) {
562 if (to == from->out_bb [i]) {
568 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
569 for (i = 0; i < from->out_count; ++i) {
570 newa [i] = from->out_bb [i];
578 for (i = 0; i < to->in_count; ++i) {
579 if (from == to->in_bb [i]) {
585 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
586 for (i = 0; i < to->in_count; ++i) {
587 newa [i] = to->in_bb [i];
596 * We mark each basic block with a region ID. We use that to avoid BB
597 * optimizations when blocks are in different regions.
600 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
602 MonoMethod *method = cfg->method;
603 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
604 MonoExceptionClause *clause;
607 /* first search for handlers and filters */
608 for (i = 0; i < header->num_clauses; ++i) {
609 clause = &header->clauses [i];
610 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->token_or_filter) &&
611 (offset < (clause->token_or_filter + filter_lengths [i])))
612 return (i << 8) | MONO_REGION_FILTER | clause->flags;
614 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
615 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
616 return (i << 8) | MONO_REGION_FINALLY | clause->flags;
618 return (i << 8) | MONO_REGION_CATCH | clause->flags;
622 /* search the try blocks */
623 for (i = 0; i < header->num_clauses; ++i) {
624 clause = &header->clauses [i];
625 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
626 return (i << 8) | clause->flags;
633 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
635 MonoMethod *method = cfg->method;
636 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
637 MonoExceptionClause *clause;
638 MonoBasicBlock *handler;
642 for (i = 0; i < header->num_clauses; ++i) {
643 clause = &header->clauses [i];
644 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
645 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
646 if (clause->flags == type) {
647 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
649 res = g_list_append (res, handler);
658 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
662 array [*dfn] = start;
663 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
664 for (i = 0; i < start->out_count; ++i) {
665 if (start->out_bb [i]->dfn)
668 start->out_bb [i]->dfn = *dfn;
669 start->out_bb [i]->df_parent = start;
670 array [*dfn] = start->out_bb [i];
671 df_visit (start->out_bb [i], dfn, array);
677 MonoBasicBlock *best;
681 previous_foreach (gconstpointer key, gpointer val, gpointer data)
683 PrevStruct *p = data;
684 MonoBasicBlock *bb = val;
685 //printf ("FIDPREV %d %p %p %p %p %p %d %d %d\n", bb->block_num, p->code, bb, p->best, bb->cil_code, p->best->cil_code,
686 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
688 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
692 static MonoBasicBlock*
693 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
699 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
704 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
713 * FIXME: take into account all the details:
714 * second may have been the target of more than one bblock
716 second->out_count = first->out_count;
717 second->out_bb = first->out_bb;
719 for (i = 0; i < first->out_count; ++i) {
720 bb = first->out_bb [i];
721 for (j = 0; j < bb->in_count; ++j) {
722 if (bb->in_bb [j] == first)
723 bb->in_bb [j] = second;
727 first->out_count = 0;
728 first->out_bb = NULL;
729 link_bblock (cfg, first, second);
731 second->last_ins = first->last_ins;
733 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
734 for (inst = first->code; inst && inst->next; inst = inst->next) {
735 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
736 g_print ("found %p: %s", inst->next->cil_code, code);
738 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
739 second->code = inst->next;
741 first->last_ins = inst;
742 second->next_bb = first->next_bb;
743 first->next_bb = second;
748 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
754 mono_type_to_ldind (MonoType *type)
766 case MONO_TYPE_BOOLEAN:
780 case MONO_TYPE_FNPTR:
782 case MONO_TYPE_CLASS:
783 case MONO_TYPE_STRING:
784 case MONO_TYPE_OBJECT:
785 case MONO_TYPE_SZARRAY:
786 case MONO_TYPE_ARRAY:
787 return CEE_LDIND_REF;
795 case MONO_TYPE_VALUETYPE:
796 if (type->data.klass->enumtype) {
797 t = type->data.klass->enum_basetype->type;
802 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
808 mono_type_to_stind (MonoType *type)
819 case MONO_TYPE_BOOLEAN:
831 case MONO_TYPE_FNPTR:
833 case MONO_TYPE_CLASS:
834 case MONO_TYPE_STRING:
835 case MONO_TYPE_OBJECT:
836 case MONO_TYPE_SZARRAY:
837 case MONO_TYPE_ARRAY:
838 return CEE_STIND_REF;
846 case MONO_TYPE_VALUETYPE:
847 if (type->data.klass->enumtype) {
848 t = type->data.klass->enum_basetype->type;
854 g_error ("unknown type %02x in type_to_stind", type->type);
860 * Returns the type used in the eval stack when @type is loaded.
861 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
864 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
868 inst->type = STACK_MP;
876 case MONO_TYPE_BOOLEAN:
882 inst->type = STACK_I4;
887 case MONO_TYPE_FNPTR:
888 inst->type = STACK_PTR;
890 case MONO_TYPE_CLASS:
891 case MONO_TYPE_STRING:
892 case MONO_TYPE_OBJECT:
893 case MONO_TYPE_SZARRAY:
894 case MONO_TYPE_ARRAY:
895 inst->type = STACK_OBJ;
899 inst->type = STACK_I8;
903 inst->type = STACK_R8;
905 case MONO_TYPE_VALUETYPE:
906 if (type->data.klass->enumtype) {
907 t = type->data.klass->enum_basetype->type;
910 inst->klass = type->data.klass;
911 inst->type = STACK_VTYPE;
915 g_error ("unknown type 0x%02x in eval stack type", type->type);
920 * The following tables are used to quickly validate the IL code in type_from_op ().
923 bin_num_table [STACK_MAX] [STACK_MAX] = {
924 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
925 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
926 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
927 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
928 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
929 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
930 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, 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}
936 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
939 /* reduce the size of this table */
941 bin_int_table [STACK_MAX] [STACK_MAX] = {
942 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
943 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
944 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
945 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
946 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
947 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
948 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
949 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
953 bin_comp_table [STACK_MAX] [STACK_MAX] = {
955 {0, 1, 0, 1, 0, 0, 4, 0},
956 {0, 0, 1, 0, 0, 0, 0, 0},
957 {0, 1, 0, 1, 0, 2, 4, 0},
958 {0, 0, 0, 0, 1, 0, 0, 0},
959 {0, 0, 0, 2, 0, 1, 0, 0},
960 {0, 4, 0, 4, 0, 0, 3, 0},
961 {0, 0, 0, 0, 0, 0, 0, 0},
964 /* reduce the size of this table */
966 shift_table [STACK_MAX] [STACK_MAX] = {
967 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
968 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
969 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
970 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
971 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
972 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
973 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
974 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
978 * Tables to map from the non-specific opcode to the matching
979 * type-specific opcode.
981 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
983 binops_op_map [STACK_MAX] = {
984 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
987 /* handles from CEE_NEG to CEE_CONV_U8 */
989 unops_op_map [STACK_MAX] = {
990 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
993 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
995 ovfops_op_map [STACK_MAX] = {
996 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
999 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1000 static const guint16
1001 ovf2ops_op_map [STACK_MAX] = {
1002 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
1005 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1006 static const guint16
1007 ovf3ops_op_map [STACK_MAX] = {
1008 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
1011 /* handles from CEE_CEQ to CEE_CLT_UN */
1012 static const guint16
1013 ceqops_op_map [STACK_MAX] = {
1014 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1018 * Sets ins->type (the type on the eval stack) according to the
1019 * type of the opcode and the arguments to it.
1020 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1022 * FIXME: this function sets ins->type unconditionally in some cases, but
1023 * it should set it to invalid for some types (a conv.x on an object)
1026 type_from_op (MonoInst *ins) {
1027 switch (ins->opcode) {
1034 /* FIXME: check unverifiable args for STACK_MP */
1035 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1036 ins->opcode += binops_op_map [ins->type];
1043 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1044 ins->opcode += binops_op_map [ins->type];
1049 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1050 ins->opcode += binops_op_map [ins->type];
1053 /* FIXME: handle some specifics with ins->next->type */
1054 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1058 case 256+CEE_CGT_UN:
1060 case 256+CEE_CLT_UN:
1061 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1062 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1066 ins->type = neg_table [ins->inst_i0->type];
1067 ins->opcode += unops_op_map [ins->type];
1070 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1071 ins->type = ins->inst_i0->type;
1073 ins->type = STACK_INV;
1074 ins->opcode += unops_op_map [ins->type];
1080 ins->type = STACK_I4;
1081 ins->opcode += unops_op_map [ins->inst_i0->type];
1084 ins->type = STACK_R8;
1085 switch (ins->inst_i0->type) {
1090 ins->opcode = OP_LCONV_TO_R_UN;
1094 case CEE_CONV_OVF_I1:
1095 case CEE_CONV_OVF_U1:
1096 case CEE_CONV_OVF_I2:
1097 case CEE_CONV_OVF_U2:
1098 case CEE_CONV_OVF_I4:
1099 case CEE_CONV_OVF_U4:
1100 ins->type = STACK_I4;
1101 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1103 case CEE_CONV_OVF_I_UN:
1104 case CEE_CONV_OVF_U_UN:
1105 ins->type = STACK_PTR;
1106 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1108 case CEE_CONV_OVF_I1_UN:
1109 case CEE_CONV_OVF_I2_UN:
1110 case CEE_CONV_OVF_I4_UN:
1111 case CEE_CONV_OVF_U1_UN:
1112 case CEE_CONV_OVF_U2_UN:
1113 case CEE_CONV_OVF_U4_UN:
1114 ins->type = STACK_I4;
1115 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1118 ins->type = STACK_PTR;
1119 switch (ins->inst_i0->type) {
1125 ins->opcode = OP_LCONV_TO_U;
1128 ins->opcode = OP_FCONV_TO_U;
1134 ins->type = STACK_I8;
1135 ins->opcode += unops_op_map [ins->inst_i0->type];
1137 case CEE_CONV_OVF_I8:
1138 case CEE_CONV_OVF_U8:
1139 ins->type = STACK_I8;
1140 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1142 case CEE_CONV_OVF_U8_UN:
1143 case CEE_CONV_OVF_I8_UN:
1144 ins->type = STACK_I8;
1145 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1149 ins->type = STACK_R8;
1150 ins->opcode += unops_op_map [ins->inst_i0->type];
1153 ins->type = STACK_R8;
1157 ins->type = STACK_I4;
1158 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1161 case CEE_CONV_OVF_I:
1162 case CEE_CONV_OVF_U:
1163 ins->type = STACK_PTR;
1164 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1167 case CEE_ADD_OVF_UN:
1169 case CEE_MUL_OVF_UN:
1171 case CEE_SUB_OVF_UN:
1172 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1173 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1176 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1183 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1186 /* map ldelem.x to the matching ldind.x opcode */
1188 ldelem_to_ldind [] = {
1202 /* map stelem.x to the matching stind.x opcode */
1204 stelem_to_stind [] = {
1218 param_table [STACK_MAX] [STACK_MAX] = {
1223 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1227 switch (args->type) {
1237 for (i = 0; i < sig->param_count; ++i) {
1238 switch (args [i].type) {
1242 if (!sig->params [i]->byref)
1246 if (sig->params [i]->byref)
1248 switch (sig->params [i]->type) {
1249 case MONO_TYPE_CLASS:
1250 case MONO_TYPE_STRING:
1251 case MONO_TYPE_OBJECT:
1252 case MONO_TYPE_SZARRAY:
1253 case MONO_TYPE_ARRAY:
1260 if (sig->params [i]->byref)
1262 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1271 /*if (!param_table [args [i].type] [sig->params [i]->type])
1279 * When we need a pointer to the current domain many times in a method, we
1280 * call mono_domain_get() once and we store the result in a local variable.
1281 * This function returns the variable that represents the MonoDomain*.
1283 inline static MonoInst *
1284 mono_get_domainvar (MonoCompile *cfg)
1286 if (!cfg->domainvar)
1287 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1288 return cfg->domainvar;
1292 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1295 int num = cfg->num_varinfo;
1297 if ((num + 1) >= cfg->varinfo_count) {
1298 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1299 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1300 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1303 mono_jit_stats.allocate_var++;
1305 MONO_INST_NEW (cfg, inst, opcode);
1306 inst->inst_c0 = num;
1307 inst->inst_vtype = type;
1308 inst->klass = mono_class_from_mono_type (type);
1309 /* if set to 1 the variable is native */
1312 cfg->varinfo [num] = inst;
1314 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1315 MONO_INIT_VARINFO (cfg->vars [num], num);
1318 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1323 type_from_stack_type (MonoInst *ins) {
1324 switch (ins->type) {
1325 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1326 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1327 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1328 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1329 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1330 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1331 case STACK_VTYPE: return &ins->klass->byval_arg;
1333 g_error ("stack type %d to montype not handled\n", ins->type);
1339 array_access_to_klass (int opcode)
1343 return mono_defaults.byte_class;
1345 return mono_defaults.uint16_class;
1348 return mono_defaults.int_class;
1351 return mono_defaults.sbyte_class;
1354 return mono_defaults.int16_class;
1357 return mono_defaults.int32_class;
1359 return mono_defaults.uint32_class;
1362 return mono_defaults.int64_class;
1365 return mono_defaults.single_class;
1368 return mono_defaults.double_class;
1369 case CEE_LDELEM_REF:
1370 case CEE_STELEM_REF:
1371 return mono_defaults.object_class;
1373 g_assert_not_reached ();
1379 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1383 MONO_ADD_INS (bb, inst);
1386 switch (bb->last_ins->opcode) {
1399 while (prev->next && prev->next != bb->last_ins)
1401 if (prev == bb->code) {
1402 if (bb->last_ins == bb->code) {
1403 inst->next = bb->code;
1406 inst->next = prev->next;
1410 inst->next = bb->last_ins;
1414 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1416 MONO_ADD_INS (bb, inst);
1422 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1424 MonoInst *inst, *load;
1426 NEW_TEMPLOAD (cfg, load, src);
1428 NEW_TEMPSTORE (cfg, inst, dest, load);
1429 if (inst->opcode == CEE_STOBJ) {
1430 NEW_TEMPLOADA (cfg, inst, dest);
1431 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1433 inst->cil_code = NULL;
1434 mono_add_ins_to_end (bb, inst);
1439 * We try to share variables when possible
1442 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1447 /* inlining can result in deeper stacks */
1448 if (slot >= ((MonoMethodNormal *)cfg->method)->header->max_stack)
1449 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1451 pos = ins->type - 1 + slot * STACK_MAX;
1453 switch (ins->type) {
1460 if ((vnum = cfg->intvars [pos]))
1461 return cfg->varinfo [vnum];
1462 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1463 cfg->intvars [pos] = res->inst_c0;
1466 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1472 * This function is called to handle items that are left on the evaluation stack
1473 * at basic block boundaries. What happens is that we save the values to local variables
1474 * and we reload them later when first entering the target basic block (with the
1475 * handle_loaded_temps () function).
1476 * A single joint point will use the same variables (stored in the array bb->out_stack or
1477 * bb->in_stack, if the basic block is before or after the joint point).
1480 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1482 MonoBasicBlock *outb;
1483 MonoInst *inst, **locals;
1487 if (cfg->verbose_level > 3)
1488 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1489 if (!bb->out_scount) {
1491 bb->out_scount = count;
1492 //g_print ("bblock %d has out:", bb->block_num);
1493 for (i = 0; i < bb->out_count; ++i) {
1494 outb = bb->out_bb [i];
1495 //g_print (" %d", outb->block_num);
1496 if (outb->in_stack) {
1498 bb->out_stack = outb->in_stack;
1504 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1505 for (i = 0; i < count; ++i) {
1507 /* try to reuse temps already allocated for this purpouse, if they occupy the same
1508 * stack slot and if they are of the same type. */
1509 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1511 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1516 locals = bb->out_stack;
1517 for (i = 0; i < count; ++i) {
1518 /* add store ops at the end of the bb, before the branch */
1519 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1520 if (inst->opcode == CEE_STOBJ) {
1521 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1522 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1524 inst->cil_code = sp [i]->cil_code;
1525 mono_add_ins_to_end (bb, inst);
1527 if (cfg->verbose_level > 3)
1528 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1531 for (i = 0; i < bb->out_count; ++i) {
1532 outb = bb->out_bb [i];
1533 if (outb->in_scount)
1534 continue; /* check they are the same locals */
1535 outb->in_scount = count;
1536 outb->in_stack = locals;
1542 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1547 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1551 case MONO_TYPE_VOID:
1552 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1555 case MONO_TYPE_BOOLEAN:
1558 case MONO_TYPE_CHAR:
1561 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1565 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1566 case MONO_TYPE_CLASS:
1567 case MONO_TYPE_STRING:
1568 case MONO_TYPE_OBJECT:
1569 case MONO_TYPE_SZARRAY:
1570 case MONO_TYPE_ARRAY:
1571 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1574 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1577 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1578 case MONO_TYPE_VALUETYPE:
1579 if (type->data.klass->enumtype) {
1580 t = type->data.klass->enum_basetype->type;
1583 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1585 g_error ("unknown type %02x in ret_type_to_call_opcode", type->type);
1591 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1593 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1595 ji->ip.label = label;
1596 ji->type = MONO_PATCH_INFO_SWITCH;
1597 ji->data.table = bbs;
1598 ji->next = cfg->patch_info;
1599 ji->table_size = num_blocks;
1600 cfg->patch_info = ji;
1604 * When we add a tree of instructions, we need to ensure the instructions currently
1605 * on the stack are executed before (like, if we load a value from a local).
1606 * We ensure this by saving the currently loaded values to temps and rewriting the
1607 * instructions to load the values.
1608 * This is not done for opcodes that terminate a basic block (because it's handled already
1609 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1612 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1614 MonoInst *load, *store, *temp, *ins;
1616 while (stack < sp) {
1618 /* handle also other constants */
1619 if (ins->opcode != OP_ICONST) {
1620 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1621 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1622 store->cil_code = ins->cil_code;
1623 if (store->opcode == CEE_STOBJ) {
1624 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1625 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1627 MONO_ADD_INS (bblock, store);
1628 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1629 load->cil_code = ins->cil_code;
1637 * Prepare arguments for passing to a function call.
1638 * Return a non-zero value if the arguments can't be passed to the given
1640 * The type checks are not yet complete and some conversions may need
1641 * casts on 32 or 64 bit architectures.
1644 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1649 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1653 for (i = 0; i < sig->param_count; ++i) {
1654 if (sig->params [i]->byref) {
1656 * check the result of ldelema is only passed as an argument if the byref
1657 * type matches exactly the array element type.
1658 * FIXME: if the argument as been saved on the stack as part of the
1659 * interface variable code (the value was on the stack at a basic block boundary)
1660 * we need to add the check in that case, too.
1662 if (args [i]->opcode == CEE_LDELEMA) {
1664 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1665 if (!exact_class->valuetype) {
1666 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1667 check->cil_code = args [i]->cil_code;
1668 check->klass = exact_class;
1669 check->inst_left = args [i]->inst_left;
1670 check->type = STACK_OBJ;
1671 args [i]->inst_left = check;
1674 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1678 simple_type = sig->params [i]->type;
1680 switch (simple_type) {
1681 case MONO_TYPE_VOID:
1686 case MONO_TYPE_BOOLEAN:
1689 case MONO_TYPE_CHAR:
1692 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1698 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1701 case MONO_TYPE_CLASS:
1702 case MONO_TYPE_STRING:
1703 case MONO_TYPE_OBJECT:
1704 case MONO_TYPE_SZARRAY:
1705 case MONO_TYPE_ARRAY:
1706 if (args [i]->type != STACK_OBJ)
1711 if (args [i]->type != STACK_I8)
1716 if (args [i]->type != STACK_R8)
1719 case MONO_TYPE_VALUETYPE:
1720 if (sig->params [i]->data.klass->enumtype) {
1721 simple_type = sig->params [i]->data.klass->enum_basetype->type;
1724 if (args [i]->type != STACK_VTYPE)
1728 g_error ("unknown type 0x%02x in check_call_signature", simple_type);
1735 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1736 const guint8 *ip, gboolean to_end)
1738 MonoInst *temp, *store, *ins = (MonoInst*)call;
1739 MonoType *ret = sig->ret;
1741 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1743 call->inst.type = STACK_OBJ;
1744 call->inst.opcode = CEE_CALL;
1745 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1747 type_to_eval_stack_type (ret, ins);
1748 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1751 if (MONO_TYPE_ISSTRUCT (ret)) {
1754 /* we use this to allocate native sized structs */
1755 temp->unused = sig->pinvoke;
1757 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1758 if (call->inst.opcode == OP_VCALL)
1759 ins->inst_left = loada;
1761 ins->inst_right = loada; /* a virtual or indirect call */
1764 mono_add_ins_to_end (bblock, ins);
1766 MONO_ADD_INS (bblock, ins);
1768 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1769 store->cil_code = ip;
1771 mono_add_ins_to_end (bblock, store);
1773 MONO_ADD_INS (bblock, store);
1775 return temp->inst_c0;
1778 mono_add_ins_to_end (bblock, ins);
1780 MONO_ADD_INS (bblock, ins);
1785 inline static MonoCallInst *
1786 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1787 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1792 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1794 call->inst.cil_code = ip;
1796 call->signature = sig;
1797 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1799 for (i = 0; i < (sig->param_count + sig->hasthis); ++i) {
1800 if (call->args [i]) {
1801 if (!call->args [i]->cil_code)
1802 call->args [i]->cil_code = ip;
1804 mono_add_ins_to_end (bblock, call->args [i]);
1806 MONO_ADD_INS (bblock, call->args [i]);
1813 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1814 MonoInst **args, MonoInst *addr, const guint8 *ip)
1816 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1818 call->inst.inst_i0 = addr;
1820 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1823 static MonoCallInst*
1824 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1825 MonoInst **args, const guint8 *ip, MonoInst *this)
1827 gboolean virtual = this != NULL;
1830 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
1832 if (this && sig->hasthis &&
1833 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
1834 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
1835 call->method = mono_marshal_get_remoting_invoke_with_check (method);
1837 call->method = method;
1839 call->inst.flags |= MONO_INST_HAS_METHOD;
1840 call->inst.inst_left = this;
1846 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
1847 MonoInst **args, const guint8 *ip, MonoInst *this)
1849 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, method->signature, args, ip, this);
1851 return mono_spill_call (cfg, bblock, call, method->signature, method->string_ctor, ip, FALSE);
1855 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
1856 MonoInst **args, const guint8 *ip, gboolean to_end)
1862 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
1864 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
1868 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
1870 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
1873 g_warning ("unregistered JIT ICall");
1874 g_assert_not_reached ();
1877 return mono_emit_native_call (cfg, bblock, info->wrapper, info->sig, args, ip, FALSE);
1881 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
1883 MonoInst *ins, *temp = NULL, *store, *load;
1887 /*g_print ("emulating: ");
1888 mono_print_tree (tree);
1890 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
1891 ins = (MonoInst*)call;
1893 call->inst.cil_code = tree->cil_code;
1895 call->signature = info->sig;
1897 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
1899 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1900 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
1901 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1902 store->cil_code = tree->cil_code;
1907 nargs = info->sig->param_count + info->sig->hasthis;
1909 for (i = 1; i < nargs; i++) {
1910 call->args [i - 1]->next = call->args [i];
1914 call->args [nargs - 1]->next = store;
1916 if (cfg->prev_ins) {
1917 store->next = cfg->prev_ins->next;
1919 cfg->prev_ins->next = call->args [0];
1921 cfg->prev_ins->next = store;
1923 store->next = cfg->cbb->code;
1925 cfg->cbb->code = call->args [0];
1927 cfg->cbb->code = store;
1931 call->fptr = info->wrapper;
1933 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1934 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1939 static MonoMethodSignature *
1940 mono_get_element_address_signature (int arity)
1942 static GHashTable *sighash = NULL;
1943 MonoMethodSignature *res;
1947 sighash = g_hash_table_new (NULL, NULL);
1950 if ((res = g_hash_table_lookup (sighash, (gpointer)arity)))
1953 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
1955 res->params [0] = &mono_defaults.array_class->byval_arg;
1957 for (i = 1; i <= arity; i++)
1958 res->params [i] = &mono_defaults.int_class->byval_arg;
1960 res->ret = &mono_defaults.int_class->byval_arg;
1962 g_hash_table_insert (sighash, (gpointer)arity, res);
1968 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
1969 MonoInst *iargs [3];
1975 * This check breaks with spilled vars... need to handle it during verification anyway.
1976 * g_assert (klass && klass == src->klass && klass == dest->klass);
1980 n = mono_class_native_size (klass, &align);
1982 n = mono_class_value_size (klass, &align);
1984 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
1986 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
1987 inst->inst_left = dest;
1988 inst->inst_right = src;
1989 inst->cil_code = ip;
1991 MONO_ADD_INS (bblock, inst);
1996 NEW_ICONST (cfg, iargs [2], n);
1998 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
2002 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2004 MonoInst *iargs [2];
2005 MonoInst *ins, *zero_int32;
2008 NEW_ICONST (cfg, zero_int32, 0);
2010 mono_class_init (klass);
2011 n = mono_class_value_size (klass, NULL);
2012 MONO_INST_NEW (cfg, ins, 0);
2014 ins->inst_left = dest;
2015 ins->inst_right = zero_int32;
2018 ins->opcode = CEE_STIND_I1;
2019 MONO_ADD_INS (bblock, ins);
2022 ins->opcode = CEE_STIND_I2;
2023 MONO_ADD_INS (bblock, ins);
2026 ins->opcode = CEE_STIND_I4;
2027 MONO_ADD_INS (bblock, ins);
2030 handle_loaded_temps (cfg, bblock, stack_start, sp);
2031 NEW_ICONST (cfg, ins, n);
2034 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2039 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2042 needs_cctor_run (MonoClass *klass, MonoMethod *caller)
2047 for (i = 0; i < klass->method.count; ++i) {
2048 method = klass->methods [i];
2049 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2050 (strcmp (".cctor", method->name) == 0)) {
2051 if (caller == method)
2060 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2062 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
2063 MonoMethodSignature *signature = method->signature;
2067 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2068 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2069 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2070 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2071 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2072 (method->klass->marshalbyref) ||
2073 !header || header->num_clauses ||
2074 /* fixme: why cant we inline valuetype returns? */
2075 MONO_TYPE_ISSTRUCT (signature->ret))
2078 /* its not worth to inline methods with valuetype arguments?? */
2079 for (i = 0; i < signature->param_count; i++) {
2080 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2086 * if we can initialize the class of the method right away, we do,
2087 * otherwise we don't allow inlining if the class needs initialization,
2088 * since it would mean inserting a call to mono_runtime_class_init()
2089 * inside the inlined code
2091 if (!(cfg->opt & MONO_OPT_SHARED)) {
2092 vtable = mono_class_vtable (cfg->domain, method->klass);
2093 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
2094 mono_runtime_class_init (vtable);
2095 else if (!vtable->initialized && needs_cctor_run (method->klass, NULL))
2099 * If we're compiling for shared code
2100 * the cctor will need to be run at aot method load time, for example,
2101 * or at the end of the compilation of the inlining method.
2103 if (needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2106 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2108 /* also consider num_locals? */
2109 if (header->code_size < 20)
2116 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2120 MonoMethodSignature *esig;
2122 rank = cmethod->signature->param_count - (is_set? 1: 0);
2124 * FIXME: handle TypeMismatch for set or use the slow path
2127 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2129 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2130 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2131 addr->inst_left = sp [0];
2132 addr->inst_right = indexes;
2133 addr->cil_code = ip;
2134 addr->type = STACK_MP;
2135 addr->klass = cmethod->klass;
2138 esig = mono_get_element_address_signature (rank);
2139 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2140 NEW_TEMPLOAD (cfg, addr, temp);
2145 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2150 if (cmethod->klass == mono_defaults.string_class) {
2151 if (cmethod->name [0] != 'g' || strcmp (cmethod->name, "get_Chars"))
2154 } else if (cmethod->klass == mono_defaults.math_class) {
2155 if (strcmp (cmethod->name, "Sin") == 0)
2157 else if (strcmp (cmethod->name, "Cos") == 0)
2159 else if (strcmp (cmethod->name, "Tan") == 0)
2161 else if (strcmp (cmethod->name, "Atan") == 0)
2163 else if (strcmp (cmethod->name, "Sqrt") == 0)
2165 else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8)
2172 pc = fsig->param_count + fsig->hasthis;
2173 MONO_INST_NEW (cfg, ins, op);
2176 ins->inst_i0 = args [0];
2178 ins->inst_i1 = args [1];
2185 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2187 MonoInst *store, *temp;
2190 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2192 if (!sig->hasthis && sig->param_count == 0)
2196 if (sp [0]->opcode == OP_ICONST) {
2199 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2201 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2202 store->cil_code = sp [0]->cil_code;
2203 MONO_ADD_INS (bblock, store);
2208 for (i = 0; i < sig->param_count; ++i) {
2209 if (sp [0]->opcode == OP_ICONST) {
2212 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2214 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2215 store->cil_code = sp [0]->cil_code;
2216 if (store->opcode == CEE_STOBJ) {
2217 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2218 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2220 MONO_ADD_INS (bblock, store);
2228 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2229 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b)
2231 MonoInst *ins, *rvar = NULL;
2232 MonoMethodHeader *cheader;
2233 MonoBasicBlock *ebblock, *sbblock;
2234 int i, costs, new_locals_offset;
2236 if (cfg->verbose_level > 2)
2237 g_print ("INLINE START %p %s\n", cmethod, mono_method_full_name (cmethod, TRUE));
2239 cheader = ((MonoMethodNormal *)cmethod)->header;
2241 if (!cmethod->inline_info) {
2242 mono_jit_stats.inlineable_methods++;
2243 cmethod->inline_info = 1;
2245 /* allocate space to store the return value */
2246 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2247 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2250 /* allocate local variables */
2251 new_locals_offset = cfg->num_varinfo;
2252 for (i = 0; i < cheader->num_locals; ++i)
2253 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2255 /* allocate starte and end blocks */
2256 sbblock = NEW_BBLOCK (cfg);
2257 sbblock->block_num = cfg->num_bblocks++;
2258 sbblock->real_offset = real_offset;
2260 ebblock = NEW_BBLOCK (cfg);
2261 ebblock->block_num = cfg->num_bblocks++;
2262 ebblock->real_offset = real_offset;
2264 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2266 if (costs >= 0 && costs < 60) {
2267 if (cfg->verbose_level > 2)
2268 g_print ("INLINE END %s\n", mono_method_full_name (cmethod, TRUE));
2270 mono_jit_stats.inlined_methods++;
2272 /* always add some code to avoid block split failures */
2273 MONO_INST_NEW (cfg, ins, CEE_NOP);
2274 MONO_ADD_INS (bblock, ins);
2277 bblock->next_bb = sbblock;
2278 link_bblock (cfg, bblock, sbblock);
2281 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2287 if (cfg->verbose_level > 2)
2288 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2294 * Some of these comments may well be out-of-date.
2295 * Design decisions: we do a single pass over the IL code (and we do bblock
2296 * splitting/merging in the few cases when it's required: a back jump to an IL
2297 * address that was not already seen as bblock starting point).
2298 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2299 * Complex operations are decomposed in simpler ones right away. We need to let the
2300 * arch-specific code peek and poke inside this process somehow (except when the
2301 * optimizations can take advantage of the full semantic info of coarse opcodes).
2302 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2303 * MonoInst->opcode initially is the IL opcode or some simplification of that
2304 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2305 * opcode with value bigger than OP_LAST.
2306 * At this point the IR can be handed over to an interpreter, a dumb code generator
2307 * or to the optimizing code generator that will translate it to SSA form.
2309 * Profiling directed optimizations.
2310 * We may compile by default with few or no optimizations and instrument the code
2311 * or the user may indicate what methods to optimize the most either in a config file
2312 * or through repeated runs where the compiler applies offline the optimizations to
2313 * each method and then decides if it was worth it.
2316 * * consider using an array instead of an hash table (bb_hash)
2319 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2320 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2321 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2323 /* offset from br.s -> br like opcodes */
2324 #define BIG_BRANCH_OFFSET 13
2327 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end)
2329 unsigned char *ip = start;
2330 unsigned char *target;
2333 MonoBasicBlock *bblock;
2334 const MonoOpcode *opcode;
2337 cli_addr = ip - start;
2338 i = mono_opcode_value (&ip);
2339 opcode = &mono_opcodes [i];
2340 switch (opcode->argument) {
2341 case MonoInlineNone:
2344 case MonoInlineString:
2345 case MonoInlineType:
2346 case MonoInlineField:
2347 case MonoInlineMethod:
2350 case MonoShortInlineR:
2357 case MonoShortInlineVar:
2358 case MonoShortInlineI:
2361 case MonoShortInlineBrTarget:
2362 target = start + cli_addr + 2 + (signed char)ip [1];
2363 GET_BBLOCK (cfg, bbhash, bblock, target);
2366 case MonoInlineBrTarget:
2367 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2368 GET_BBLOCK (cfg, bbhash, bblock, target);
2371 case MonoInlineSwitch: {
2372 guint32 n = read32 (ip + 1);
2375 cli_addr += 5 + 4 * n;
2376 target = start + cli_addr;
2377 GET_BBLOCK (cfg, bbhash, bblock, target);
2379 for (j = 0; j < n; ++j) {
2380 target = start + cli_addr + (gint32)read32 (ip);
2381 GET_BBLOCK (cfg, bbhash, bblock, target);
2391 g_assert_not_reached ();
2400 * mono_method_to_ir: translates IL into basic blocks containing trees
2403 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2404 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2405 guint inline_offset, gboolean is_virtual_call)
2407 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2408 MonoInst *ins, **sp, **stack_start;
2409 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2411 MonoMethod *cmethod;
2412 MonoInst **arg_array;
2413 MonoMethodHeader *header;
2415 guint32 token, ins_flag;
2417 unsigned char *ip, *end, *target;
2418 static double r8_0 = 0.0;
2419 MonoMethodSignature *sig;
2420 MonoType **param_types;
2421 GList *bb_recheck = NULL, *tmp;
2422 int i, n, start_new_bblock, align;
2423 int num_calls = 0, inline_costs = 0;
2424 int *filter_lengths = NULL;
2425 int breakpoint_id = 0;
2428 image = method->klass->image;
2429 header = ((MonoMethodNormal *)method)->header;
2430 sig = method->signature;
2431 ip = (unsigned char*)header->code;
2432 end = ip + header->code_size;
2433 mono_jit_stats.cil_code_size += header->code_size;
2435 if (cfg->method == method) {
2437 bbhash = cfg->bb_hash;
2439 real_offset = inline_offset;
2440 bbhash = g_hash_table_new (g_direct_hash, NULL);
2443 dont_inline = g_list_prepend (dont_inline, method);
2444 if (cfg->method == method) {
2447 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2448 start_bblock->cil_code = NULL;
2449 start_bblock->cil_length = 0;
2450 start_bblock->block_num = cfg->num_bblocks++;
2453 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2454 end_bblock->cil_code = NULL;
2455 end_bblock->cil_length = 0;
2456 end_bblock->block_num = cfg->num_bblocks++;
2457 g_assert (cfg->num_bblocks == 2);
2459 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2460 for (i = sig->hasthis + sig->param_count - 1; i >= 0; i--)
2461 arg_array [i] = cfg->varinfo [i];
2463 if (mono_compile_aot)
2464 cfg->opt |= MONO_OPT_SHARED;
2466 if (header->num_clauses) {
2467 int size = sizeof (int) * header->num_clauses;
2468 filter_lengths = alloca (size);
2469 memset (filter_lengths, 0, size);
2471 cfg->spvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2472 /* prevent it from being register allocated */
2473 cfg->spvar->flags |= MONO_INST_INDIRECT;
2475 /* handle exception clauses */
2476 for (i = 0; i < header->num_clauses; ++i) {
2477 //unsigned char *p = ip;
2478 MonoExceptionClause *clause = &header->clauses [i];
2479 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2480 tblock->real_offset = clause->try_offset;
2481 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2482 tblock->real_offset = clause->handler_offset;
2484 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2485 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2486 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2487 MONO_ADD_INS (tblock, ins);
2490 /*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);
2492 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2494 /* catch and filter blocks get the exception object on the stack */
2495 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2496 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2497 /* mostly like handle_stack_args (), but just sets the input args */
2498 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2500 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2501 /* prevent it from being register allocated */
2502 cfg->exvar->flags |= MONO_INST_INDIRECT;
2504 tblock->in_scount = 1;
2505 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2506 tblock->in_stack [0] = cfg->exvar;
2508 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2509 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2510 tblock->real_offset = clause->token_or_filter;
2511 tblock->in_scount = 1;
2512 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2513 tblock->in_stack [0] = cfg->exvar;
2514 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2515 MONO_ADD_INS (tblock, ins);
2521 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2522 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2525 /* FIRST CODE BLOCK */
2526 bblock = NEW_BBLOCK (cfg);
2527 bblock->cil_code = ip;
2529 ADD_BBLOCK (cfg, bbhash, bblock);
2531 if (cfg->method == method) {
2532 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2533 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2534 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2535 MONO_ADD_INS (bblock, ins);
2539 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED)))) {
2540 /* we use a separate basic block for the initialization code */
2541 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2542 init_localsbb->real_offset = real_offset;
2543 start_bblock->next_bb = init_localsbb;
2544 init_localsbb->next_bb = bblock;
2545 link_bblock (cfg, start_bblock, init_localsbb);
2546 link_bblock (cfg, init_localsbb, bblock);
2547 init_localsbb->block_num = cfg->num_bblocks++;
2549 start_bblock->next_bb = bblock;
2550 link_bblock (cfg, start_bblock, bblock);
2553 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end))
2556 mono_debug_init_method (cfg, bblock, breakpoint_id);
2558 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * (sig->hasthis + sig->param_count));
2560 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2561 for (n = 0; n < sig->param_count; ++n)
2562 param_types [n + sig->hasthis] = sig->params [n];
2564 /* do this somewhere outside - not here */
2565 NEW_ICONST (cfg, zero_int32, 0);
2566 NEW_ICONST (cfg, zero_int64, 0);
2567 zero_int64->type = STACK_I8;
2568 NEW_PCONST (cfg, zero_ptr, 0);
2569 NEW_PCONST (cfg, zero_obj, 0);
2570 zero_obj->type = STACK_OBJ;
2572 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2573 zero_r8->type = STACK_R8;
2574 zero_r8->inst_p0 = &r8_0;
2576 /* add a check for this != NULL to inlined methods */
2577 if (is_virtual_call) {
2578 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2579 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2581 MONO_ADD_INS (bblock, ins);
2584 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2585 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2588 start_new_bblock = 0;
2591 if (cfg->method == method)
2592 real_offset = ip - header->code;
2594 real_offset = inline_offset;
2596 if (start_new_bblock) {
2597 bblock->cil_length = ip - bblock->cil_code;
2598 if (start_new_bblock == 2) {
2599 g_assert (ip == tblock->cil_code);
2601 GET_BBLOCK (cfg, bbhash, tblock, ip);
2603 bblock->next_bb = tblock;
2605 start_new_bblock = 0;
2606 for (i = 0; i < bblock->in_scount; ++i) {
2607 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2611 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2612 link_bblock (cfg, bblock, tblock);
2613 if (sp != stack_start) {
2614 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2617 bblock->next_bb = tblock;
2619 for (i = 0; i < bblock->in_scount; ++i) {
2620 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2626 if (cfg->verbose_level > 3)
2627 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2634 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2635 ins->cil_code = ip++;
2636 MONO_ADD_INS (bblock, ins);
2642 CHECK_STACK_OVF (1);
2643 n = (*ip)-CEE_LDARG_0;
2644 NEW_ARGLOAD (cfg, ins, n);
2645 ins->cil_code = ip++;
2652 CHECK_STACK_OVF (1);
2653 n = (*ip)-CEE_LDLOC_0;
2654 NEW_LOCLOAD (cfg, ins, n);
2655 ins->cil_code = ip++;
2663 n = (*ip)-CEE_STLOC_0;
2665 handle_loaded_temps (cfg, bblock, stack_start, sp);
2666 NEW_LOCSTORE (cfg, ins, n, *sp);
2668 if (ins->opcode == CEE_STOBJ) {
2669 NEW_LOCLOADA (cfg, ins, n);
2670 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2672 MONO_ADD_INS (bblock, ins);
2677 CHECK_STACK_OVF (1);
2678 NEW_ARGLOAD (cfg, ins, ip [1]);
2684 CHECK_STACK_OVF (1);
2685 NEW_ARGLOADA (cfg, ins, ip [1]);
2693 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2694 handle_loaded_temps (cfg, bblock, stack_start, sp);
2696 if (ins->opcode == CEE_STOBJ) {
2697 NEW_ARGLOADA (cfg, ins, ip [1]);
2698 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2700 MONO_ADD_INS (bblock, ins);
2704 CHECK_STACK_OVF (1);
2705 NEW_LOCLOAD (cfg, ins, ip [1]);
2711 CHECK_STACK_OVF (1);
2712 NEW_LOCLOADA (cfg, ins, ip [1]);
2720 handle_loaded_temps (cfg, bblock, stack_start, sp);
2721 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
2723 if (ins->opcode == CEE_STOBJ) {
2724 NEW_LOCLOADA (cfg, ins, ip [1]);
2725 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2727 MONO_ADD_INS (bblock, ins);
2732 CHECK_STACK_OVF (1);
2733 NEW_PCONST (cfg, ins, NULL);
2735 ins->type = STACK_OBJ;
2740 CHECK_STACK_OVF (1);
2741 NEW_ICONST (cfg, ins, -1);
2755 CHECK_STACK_OVF (1);
2756 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
2762 CHECK_STACK_OVF (1);
2764 NEW_ICONST (cfg, ins, *((signed char*)ip));
2770 CHECK_STACK_OVF (1);
2771 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
2777 CHECK_STACK_OVF (1);
2778 MONO_INST_NEW (cfg, ins, OP_I8CONST);
2780 ins->type = STACK_I8;
2782 ins->inst_l = (gint64)read64 (ip);
2787 float *f = g_malloc (sizeof (float));
2788 CHECK_STACK_OVF (1);
2789 MONO_INST_NEW (cfg, ins, OP_R4CONST);
2790 ins->type = STACK_R8;
2799 double *d = g_malloc (sizeof (double));
2800 CHECK_STACK_OVF (1);
2801 MONO_INST_NEW (cfg, ins, OP_R8CONST);
2802 ins->type = STACK_R8;
2811 MonoInst *temp, *store;
2813 CHECK_STACK_OVF (1);
2818 * small optimization: if the loaded value was from a local already,
2819 * just load it twice.
2821 if (ins->ssa_op == MONO_SSA_LOAD &&
2822 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
2824 MONO_INST_NEW (cfg, temp, 0);
2826 temp->cil_code = ip;
2829 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2830 temp->cil_code = ip;
2831 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2832 store->cil_code = ip;
2833 MONO_ADD_INS (bblock, store);
2834 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2837 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2847 MONO_INST_NEW (cfg, ins, CEE_POP);
2848 MONO_ADD_INS (bblock, ins);
2849 ins->cil_code = ip++;
2854 if (stack_start != sp)
2856 MONO_INST_NEW (cfg, ins, CEE_JMP);
2857 token = read32 (ip + 1);
2858 /* FIXME: check the signature matches */
2859 cmethod = mono_get_method (image, token, NULL);
2860 ins->inst_p0 = cmethod;
2861 MONO_ADD_INS (bblock, ins);
2863 start_new_bblock = 1;
2867 case CEE_CALLVIRT: {
2868 MonoInst *addr = NULL;
2869 MonoMethodSignature *fsig = NULL;
2870 int temp, array_rank = 0;
2871 int virtual = *ip == CEE_CALLVIRT;
2873 token = read32 (ip + 1);
2875 if (*ip == CEE_CALLI) {
2880 if (method->wrapper_type != MONO_WRAPPER_NONE)
2881 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
2883 fsig = mono_metadata_parse_signature (image, token);
2885 n = fsig->param_count + fsig->hasthis;
2888 cmethod = mono_get_method (image, token, NULL);
2890 if (!cmethod->klass->inited)
2891 mono_class_init (cmethod->klass);
2893 if (cmethod->signature->pinvoke) {
2894 #ifdef MONO_USE_EXC_TABLES
2895 if (mono_method_blittable (cmethod)) {
2896 fsig = cmethod->signature;
2899 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
2900 fsig = wrapper->signature;
2901 #ifdef MONO_USE_EXC_TABLES
2905 fsig = cmethod->signature;
2908 n = fsig->param_count + fsig->hasthis;
2910 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
2911 cmethod->klass->parent == mono_defaults.array_class) {
2912 array_rank = cmethod->klass->rank;
2915 if (cmethod->string_ctor)
2916 g_assert_not_reached ();
2922 //g_assert (!virtual || fsig->hasthis);
2926 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
2929 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL)) {
2931 for (i = 0; i < n; ++i) {
2932 NEW_ARGSTORE (cfg, ins, i, sp [i]);
2934 MONO_ADD_INS (bblock, ins);
2936 MONO_INST_NEW (cfg, ins, CEE_JMP);
2938 ins->inst_p0 = cmethod;
2939 MONO_ADD_INS (bblock, ins);
2940 start_new_bblock = 1;
2941 /* skip CEE_RET as well */
2946 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
2949 if (MONO_TYPE_IS_VOID (fsig->ret)) {
2950 MONO_ADD_INS (bblock, ins);
2952 type_to_eval_stack_type (fsig->ret, ins);
2961 handle_loaded_temps (cfg, bblock, stack_start, sp);
2963 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
2964 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
2965 mono_method_check_inlining (cfg, cmethod) &&
2966 !g_list_find (dont_inline, cmethod)) {
2968 MonoBasicBlock *ebblock;
2970 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
2974 GET_BBLOCK (cfg, bbhash, bblock, ip);
2975 ebblock->next_bb = bblock;
2976 link_bblock (cfg, ebblock, bblock);
2978 if (!MONO_TYPE_IS_VOID (fsig->ret))
2981 /* indicates start of a new block, and triggers a load of all
2982 stack arguments at bb boundarie */
2985 inline_costs += costs;
2990 inline_costs += 10 * num_calls++;
2992 /* tail recursion elimination */
2993 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == cfg->method && ip [5] == CEE_RET) {
2994 gboolean has_vtargs = FALSE;
2997 /* keep it simple */
2998 for (i = fsig->param_count - 1; i >= 0; i--) {
2999 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3004 for (i = 0; i < n; ++i) {
3005 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3007 MONO_ADD_INS (bblock, ins);
3009 MONO_INST_NEW (cfg, ins, CEE_BR);
3011 MONO_ADD_INS (bblock, ins);
3012 tblock = start_bblock->out_bb [0];
3013 link_bblock (cfg, bblock, tblock);
3014 ins->inst_target_bb = tblock;
3015 start_new_bblock = 1;
3018 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3019 /* just create a dummy - the value is never used */
3020 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3021 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3029 if (*ip == CEE_CALLI) {
3031 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3032 NEW_TEMPLOAD (cfg, *sp, temp);
3036 } else if (array_rank) {
3039 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3040 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3041 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3043 if (ins->opcode == CEE_STOBJ) {
3044 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3046 MONO_ADD_INS (bblock, ins);
3049 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3050 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3051 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3055 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3056 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3059 g_assert_not_reached ();
3063 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3064 /* no need to spill */
3065 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3068 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3069 NEW_TEMPLOAD (cfg, *sp, temp);
3079 if (cfg->method != method) {
3080 /* return from inlined methode */
3085 //g_assert (returnvar != -1);
3086 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3087 store->cil_code = sp [0]->cil_code;
3088 if (store->opcode == CEE_STOBJ) {
3089 g_assert_not_reached ();
3090 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3091 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3093 MONO_ADD_INS (bblock, store);
3097 g_assert (!return_var);
3100 MONO_INST_NEW (cfg, ins, CEE_NOP);
3101 ins->opcode = mono_type_to_stind (method->signature->ret);
3102 if (ins->opcode == CEE_STOBJ) {
3103 NEW_RETLOADA (cfg, ins);
3104 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3106 ins->opcode = OP_SETRET;
3108 ins->inst_i0 = *sp;;
3109 ins->inst_i1 = NULL;
3110 MONO_ADD_INS (bblock, ins);
3114 if (sp != stack_start)
3116 MONO_INST_NEW (cfg, ins, CEE_BR);
3117 ins->cil_code = ip++;
3118 ins->inst_target_bb = end_bblock;
3119 MONO_ADD_INS (bblock, ins);
3120 link_bblock (cfg, bblock, end_bblock);
3121 start_new_bblock = 1;
3124 MONO_INST_NEW (cfg, ins, CEE_BR);
3125 ins->cil_code = ip++;
3126 MONO_ADD_INS (bblock, ins);
3127 target = ip + 1 + (signed char)(*ip);
3129 GET_BBLOCK (cfg, bbhash, tblock, target);
3130 link_bblock (cfg, bblock, tblock);
3131 CHECK_BBLOCK (target, ip, tblock);
3132 ins->inst_target_bb = tblock;
3133 if (sp != stack_start) {
3134 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3137 start_new_bblock = 1;
3143 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3144 ins->cil_code = ip++;
3145 target = ip + 1 + *(signed char*)ip;
3147 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3148 if (sp != stack_start) {
3149 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3165 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3166 ins->cil_code = ip++;
3167 target = ip + 1 + *(signed char*)ip;
3170 if (sp != stack_start) {
3171 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3177 MONO_INST_NEW (cfg, ins, CEE_BR);
3178 ins->cil_code = ip++;
3179 MONO_ADD_INS (bblock, ins);
3180 target = ip + 4 + (gint32)read32(ip);
3182 GET_BBLOCK (cfg, bbhash, tblock, target);
3183 link_bblock (cfg, bblock, tblock);
3184 CHECK_BBLOCK (target, ip, tblock);
3185 ins->inst_target_bb = tblock;
3186 if (sp != stack_start) {
3187 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3190 start_new_bblock = 1;
3196 MONO_INST_NEW (cfg, ins, *ip);
3197 ins->cil_code = ip++;
3198 target = ip + 4 + (gint32)read32(ip);
3200 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3201 if (sp != stack_start) {
3202 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3218 MONO_INST_NEW (cfg, ins, *ip);
3219 ins->cil_code = ip++;
3220 target = ip + 4 + (gint32)read32(ip);
3223 if (sp != stack_start) {
3224 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3231 n = read32 (ip + 1);
3232 MONO_INST_NEW (cfg, ins, *ip);
3234 ins->inst_left = *sp;
3235 if (ins->inst_left->type != STACK_I4) goto unverified;
3238 target = ip + n * sizeof (guint32);
3239 MONO_ADD_INS (bblock, ins);
3240 GET_BBLOCK (cfg, bbhash, tblock, target);
3241 link_bblock (cfg, bblock, tblock);
3242 ins->klass = GUINT_TO_POINTER (n);
3243 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3244 ins->inst_many_bb [n] = tblock;
3246 for (i = 0; i < n; ++i) {
3247 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3248 link_bblock (cfg, bblock, tblock);
3249 ins->inst_many_bb [i] = tblock;
3252 /* FIXME: handle stack args */
3267 MONO_INST_NEW (cfg, ins, *ip);
3272 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3273 ins->flags |= ins_flag;
3285 MONO_INST_NEW (cfg, ins, *ip);
3286 ins->cil_code = ip++;
3288 handle_loaded_temps (cfg, bblock, stack_start, sp);
3289 MONO_ADD_INS (bblock, ins);
3290 ins->inst_i0 = sp [0];
3291 ins->inst_i1 = sp [1];
3292 ins->flags |= ins_flag;
3323 case CEE_CONV_OVF_I8:
3324 case CEE_CONV_OVF_U8:
3330 case CEE_CONV_OVF_I4:
3331 case CEE_CONV_OVF_I1:
3332 case CEE_CONV_OVF_I2:
3333 case CEE_CONV_OVF_I:
3334 case CEE_CONV_OVF_U:
3337 if (sp [-1]->type == STACK_R8) {
3338 ADD_UNOP (CEE_CONV_OVF_I8);
3346 case CEE_CONV_OVF_U1:
3347 case CEE_CONV_OVF_U2:
3348 case CEE_CONV_OVF_U4:
3351 if (sp [-1]->type == STACK_R8) {
3352 ADD_UNOP (CEE_CONV_OVF_U8);
3360 case CEE_CONV_OVF_I1_UN:
3361 case CEE_CONV_OVF_I2_UN:
3362 case CEE_CONV_OVF_I4_UN:
3363 case CEE_CONV_OVF_I8_UN:
3364 case CEE_CONV_OVF_U1_UN:
3365 case CEE_CONV_OVF_U2_UN:
3366 case CEE_CONV_OVF_U4_UN:
3367 case CEE_CONV_OVF_U8_UN:
3368 case CEE_CONV_OVF_I_UN:
3369 case CEE_CONV_OVF_U_UN:
3375 g_error ("opcode 0x%02x not handled", *ip);
3378 MonoInst *iargs [3];
3381 token = read32 (ip + 1);
3382 if (method->wrapper_type != MONO_WRAPPER_NONE)
3383 klass = mono_method_get_wrapper_data (method, token);
3385 klass = mono_class_get (image, token);
3387 mono_class_init (klass);
3388 n = mono_class_value_size (klass, NULL);
3389 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3390 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3391 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3393 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3394 copy->inst_left = iargs [0];
3395 copy->inst_right = *sp;
3396 copy->cil_code = ip;
3398 MONO_ADD_INS (bblock, copy);
3401 NEW_ICONST (cfg, iargs [2], n);
3402 iargs [2]->cil_code = ip;
3404 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3406 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3413 CHECK_STACK_OVF (1);
3414 n = read32 (ip + 1);
3416 if (mono_compile_aot) {
3417 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3420 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
3422 MonoInst *iargs [3];
3423 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3424 NEW_IMAGECONST (cfg, iargs [1], image);
3425 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3426 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3427 NEW_TEMPLOAD (cfg, *sp, temp);
3428 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3430 NEW_PCONST (cfg, ins, NULL);
3432 ins->type = STACK_OBJ;
3433 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3440 MonoInst *iargs [2];
3443 token = read32 (ip + 1);
3444 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3445 cmethod = mono_method_get_wrapper_data (method, token);
3447 cmethod = mono_get_method (image, token, NULL);
3449 mono_class_init (cmethod->klass);
3451 n = cmethod->signature->param_count;
3454 /* move the args to allow room for 'this' in the first position */
3460 handle_loaded_temps (cfg, bblock, stack_start, sp);
3463 if (cmethod->klass->parent == mono_defaults.array_class) {
3464 NEW_METHODCONST (cfg, *sp, cmethod);
3465 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, cmethod->signature, sp, ip, FALSE);
3467 } else if (cmethod->string_ctor) {
3468 /* we simply pass a null pointer */
3469 NEW_PCONST (cfg, *sp, NULL);
3470 /* now call the string ctor */
3471 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, sp, ip, NULL);
3473 if (cmethod->klass->valuetype) {
3474 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3475 temp = iargs [0]->inst_c0;
3476 NEW_TEMPLOADA (cfg, *sp, temp);
3478 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
3479 NEW_DOMAINCONST (cfg, iargs [0]);
3480 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3482 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3484 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3485 NEW_PCONST (cfg, iargs [0], vtable);
3486 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3488 NEW_TEMPLOAD (cfg, *sp, temp);
3491 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3492 mono_method_check_inlining (cfg, cmethod) &&
3493 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
3494 !g_list_find (dont_inline, cmethod)) {
3496 MonoBasicBlock *ebblock;
3497 if ((costs = inline_method (cfg, cmethod, cmethod->signature, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3502 GET_BBLOCK (cfg, bbhash, bblock, ip);
3503 ebblock->next_bb = bblock;
3504 link_bblock (cfg, ebblock, bblock);
3506 NEW_TEMPLOAD (cfg, *sp, temp);
3509 /* indicates start of a new block, and triggers a load
3510 of all stack arguments at bb boundarie */
3513 inline_costs += costs;
3517 mono_emit_method_call_spilled (cfg, bblock, cmethod, sp, ip, sp[0]);
3520 /* now call the actual ctor */
3521 mono_emit_method_call_spilled (cfg, bblock, cmethod, sp, ip, sp[0]);
3525 NEW_TEMPLOAD (cfg, *sp, temp);
3534 MONO_INST_NEW (cfg, ins, *ip);
3536 klass = mono_class_get (image, read32 (ip + 1));
3537 mono_class_init (klass);
3538 ins->type = STACK_OBJ;
3539 ins->inst_left = *sp;
3540 ins->inst_newa_class = klass;
3546 MonoInst *add, *vtoffset;
3550 token = read32 (ip + 1);
3551 if (method->wrapper_type != MONO_WRAPPER_NONE)
3552 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3554 klass = mono_class_get (image, token);
3555 mono_class_init (klass);
3558 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
3559 ins->type = STACK_OBJ;
3560 ins->inst_left = *sp;
3562 ins->inst_newa_class = klass;
3565 MONO_INST_NEW (cfg, add, CEE_ADD);
3566 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3567 add->inst_left = ins;
3568 add->inst_right = vtoffset;
3569 add->type = STACK_MP;
3577 MONO_INST_NEW (cfg, ins, *ip);
3579 klass = mono_class_get (image, read32 (ip + 1));
3580 mono_class_init (klass);
3581 ins->type = STACK_OBJ;
3582 ins->inst_left = *sp;
3584 ins->inst_newa_class = klass;
3591 MONO_INST_NEW (cfg, ins, *ip);
3593 ins->inst_left = *sp;
3594 ins->cil_code = ip++;
3595 MONO_ADD_INS (bblock, ins);
3597 start_new_bblock = 1;
3602 MonoInst *offset_ins;
3603 MonoClassField *field;
3604 MonoBasicBlock *ebblock;
3608 if (*ip == CEE_STFLD) {
3615 // FIXME: enable this test later.
3616 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
3618 token = read32 (ip + 1);
3619 field = mono_field_from_token (image, token, &klass);
3620 mono_class_init (klass);
3622 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
3623 /* FIXME: mark instructions for use in SSA */
3624 if (*ip == CEE_STFLD) {
3625 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3626 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
3627 MonoInst *iargs [5];
3630 NEW_CLASSCONST (cfg, iargs [1], klass);
3631 NEW_FIELDCONST (cfg, iargs [2], field);
3632 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
3636 if (cfg->opt & MONO_OPT_INLINE) {
3637 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
3638 iargs, ip, real_offset, dont_inline, &ebblock);
3639 g_assert (costs > 0);
3644 GET_BBLOCK (cfg, bbhash, bblock, ip);
3645 ebblock->next_bb = bblock;
3646 link_bblock (cfg, ebblock, bblock);
3648 /* indicates start of a new block, and triggers a load
3649 of all stack arguments at bb boundarie */
3652 inline_costs += costs;
3655 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, iargs, ip, NULL);
3659 NEW_ICONST (cfg, offset_ins, foffset);
3660 MONO_INST_NEW (cfg, ins, CEE_ADD);
3662 ins->inst_left = *sp;
3663 ins->inst_right = offset_ins;
3664 ins->type = STACK_MP;
3666 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3667 store->cil_code = ip;
3668 store->inst_left = ins;
3669 store->inst_right = sp [1];
3670 handle_loaded_temps (cfg, bblock, stack_start, sp);
3671 store->flags |= ins_flag;
3673 if (store->opcode == CEE_STOBJ) {
3674 handle_stobj (cfg, bblock, ins, sp [1], ip,
3675 mono_class_from_mono_type (field->type), FALSE, FALSE);
3677 MONO_ADD_INS (bblock, store);
3680 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3681 /* fixme: we need to inline that call somehow */
3682 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
3683 MonoInst *iargs [4];
3687 NEW_CLASSCONST (cfg, iargs [1], klass);
3688 NEW_FIELDCONST (cfg, iargs [2], field);
3689 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
3690 if (cfg->opt & MONO_OPT_INLINE) {
3691 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
3692 iargs, ip, real_offset, dont_inline, &ebblock);
3693 g_assert (costs > 0);
3698 GET_BBLOCK (cfg, bbhash, bblock, ip);
3699 ebblock->next_bb = bblock;
3700 link_bblock (cfg, ebblock, bblock);
3702 temp = iargs [0]->inst_i0->inst_c0;
3704 if (*ip == CEE_LDFLDA) {
3705 /* not sure howto handle this */
3706 NEW_TEMPLOADA (cfg, *sp, temp);
3708 NEW_TEMPLOAD (cfg, *sp, temp);
3712 /* indicates start of a new block, and triggers a load of
3713 all stack arguments at bb boundarie */
3716 inline_costs += costs;
3719 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, iargs, ip, NULL);
3720 if (*ip == CEE_LDFLDA) {
3721 /* not sure howto handle this */
3722 NEW_TEMPLOADA (cfg, *sp, temp);
3724 NEW_TEMPLOAD (cfg, *sp, temp);
3729 NEW_ICONST (cfg, offset_ins, foffset);
3730 MONO_INST_NEW (cfg, ins, CEE_ADD);
3732 ins->inst_left = *sp;
3733 ins->inst_right = offset_ins;
3734 ins->type = STACK_MP;
3736 if (*ip == CEE_LDFLDA) {
3740 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3741 type_to_eval_stack_type (field->type, load);
3742 load->cil_code = ip;
3743 load->inst_left = ins;
3744 load->flags |= ins_flag;
3756 MonoClassField *field;
3758 token = read32 (ip + 1);
3760 field = mono_field_from_token (image, token, &klass);
3761 mono_class_init (klass);
3763 handle_loaded_temps (cfg, bblock, stack_start, sp);
3765 if (((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)) {
3767 MonoInst *iargs [2];
3768 g_assert (field->parent);
3769 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3770 NEW_FIELDCONST (cfg, iargs [1], field);
3771 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
3772 NEW_TEMPLOAD (cfg, ins, temp);
3776 vtable = mono_class_vtable (cfg->domain, klass);
3777 if (!cfg->domain->thread_static_fields || !(addr = g_hash_table_lookup (cfg->domain->thread_static_fields, field))) {
3778 if (!vtable->initialized && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && needs_cctor_run (klass, method)) {
3779 MonoInst *iargs [1];
3780 NEW_PCONST (cfg, iargs [0], vtable);
3781 mono_emit_jit_icall (cfg, bblock, mono_runtime_class_init, iargs, ip);
3782 if (cfg->verbose_level > 2)
3783 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
3785 mono_runtime_class_init (vtable);
3787 addr = (char*)vtable->data + field->offset;
3788 NEW_PCONST (cfg, ins, addr);
3792 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
3793 * This could be later optimized to do just a couple of
3794 * memory dereferences with constant offsets.
3797 MonoInst *iargs [1];
3798 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
3799 temp = mono_emit_jit_icall (cfg, bblock, mono_threads_get_static_data, iargs, ip);
3800 NEW_TEMPLOAD (cfg, ins, temp);
3804 /* FIXME: mark instructions for use in SSA */
3805 if (*ip == CEE_LDSFLDA) {
3807 } else if (*ip == CEE_STSFLD) {
3811 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3812 store->cil_code = ip;
3813 store->inst_left = ins;
3814 store->inst_right = sp [0];
3815 store->flags |= ins_flag;
3818 if (store->opcode == CEE_STOBJ) {
3819 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
3821 MONO_ADD_INS (bblock, store);
3824 CHECK_STACK_OVF (1);
3825 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3826 type_to_eval_stack_type (field->type, load);
3827 load->cil_code = ip;
3828 load->inst_left = ins;
3830 load->flags |= ins_flag;
3832 /* fixme: dont see the problem why this does not work */
3833 //cfg->disable_aot = TRUE;
3841 token = read32 (ip + 1);
3842 if (method->wrapper_type != MONO_WRAPPER_NONE)
3843 klass = mono_method_get_wrapper_data (method, token);
3845 klass = mono_class_get (image, token);
3846 mono_class_init (klass);
3847 n = mono_type_to_stind (&klass->byval_arg);
3848 if (n == CEE_STOBJ) {
3849 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
3851 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
3853 MONO_INST_NEW (cfg, store, n);
3854 store->cil_code = ip;
3855 store->inst_left = sp [0];
3856 store->inst_right = sp [1];
3857 store->flags |= ins_flag;
3858 MONO_ADD_INS (bblock, store);
3865 MonoInst *iargs [2];
3866 MonoInst *load, *vtoffset, *add, *val, *vstore;
3871 token = read32 (ip + 1);
3872 if (method->wrapper_type != MONO_WRAPPER_NONE)
3873 klass = mono_method_get_wrapper_data (method, token);
3875 klass = mono_class_get (image, token);
3876 mono_class_init (klass);
3878 /* much like NEWOBJ */
3879 NEW_DOMAINCONST (cfg, iargs [0]);
3880 NEW_CLASSCONST (cfg, iargs [1], klass);
3882 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3883 NEW_TEMPLOAD (cfg, load, temp);
3884 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3885 MONO_INST_NEW (cfg, add, CEE_ADD);
3886 add->inst_left = load;
3887 add->inst_right = vtoffset;
3890 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
3891 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
3892 vstore->cil_code = ip;
3893 vstore->inst_left = add;
3894 vstore->inst_right = val;
3896 if (vstore->opcode == CEE_STOBJ) {
3897 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
3899 MONO_ADD_INS (bblock, vstore);
3901 NEW_TEMPLOAD (cfg, load, temp);
3909 MONO_INST_NEW (cfg, ins, *ip);
3913 token = read32 (ip + 1);
3915 /* allocate the domainvar - becaus this is used in decompose_foreach */
3916 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)
3917 mono_get_domainvar (cfg);
3919 if (method->wrapper_type != MONO_WRAPPER_NONE)
3920 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3922 klass = mono_class_get (image, token);
3924 mono_class_init (klass);
3925 ins->inst_newa_class = klass;
3926 ins->inst_newa_len = *sp;
3927 ins->type = STACK_OBJ;
3934 MONO_INST_NEW (cfg, ins, *ip);
3935 ins->cil_code = ip++;
3937 ins->inst_left = *sp;
3938 ins->type = STACK_PTR;
3944 klass = mono_class_get (image, read32 (ip + 1));
3945 mono_class_init (klass);
3946 NEW_LDELEMA (cfg, ins, sp, klass);
3961 case CEE_LDELEM_REF: {
3965 * ldind.x (ldelema (array, index))
3966 * ldelema does the bounds check
3970 klass = array_access_to_klass (*ip);
3971 NEW_LDELEMA (cfg, load, sp, klass);
3972 load->cil_code = ip;
3973 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
3975 ins->inst_left = load;
3977 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
3987 case CEE_STELEM_R8: {
3991 * stind.x (ldelema (array, index), val)
3992 * ldelema does the bounds check
3996 klass = array_access_to_klass (*ip);
3997 NEW_LDELEMA (cfg, load, sp, klass);
3998 load->cil_code = ip;
3999 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4001 ins->inst_left = load;
4002 ins->inst_right = sp [2];
4004 handle_loaded_temps (cfg, bblock, stack_start, sp);
4005 MONO_ADD_INS (bblock, ins);
4006 /* FIXME: add the implicit STELEM_REF castclass */
4008 cfg->disable_ssa = TRUE;
4011 case CEE_STELEM_REF: {
4012 MonoInst *iargs [3];
4017 handle_loaded_temps (cfg, bblock, stack_start, sp);
4023 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4027 NEW_GROUP (cfg, group, sp [0], sp [1]);
4028 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4030 ins->inst_left = group;
4031 ins->inst_right = sp [2];
4032 MONO_ADD_INS (bblock, ins);
4037 cfg->disable_ssa = TRUE;
4040 case CEE_CKFINITE: {
4041 MonoInst *store, *temp;
4044 /* this instr. can throw exceptions as side effect,
4045 * so we cant eliminate dead code which contains CKFINITE opdodes.
4046 * Spilling to memory makes sure that we always perform
4050 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4052 ins->inst_left = sp [-1];
4053 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4055 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4056 store->cil_code = ip;
4057 MONO_ADD_INS (bblock, store);
4059 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4066 g_error ("opcode 0x%02x not handled", *ip);
4070 MonoClass *handle_class;
4072 CHECK_STACK_OVF (1);
4074 n = read32 (ip + 1);
4076 handle = mono_ldtoken (image, n, &handle_class);
4077 mono_class_init (handle_class);
4079 if (((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)) {
4081 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4083 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4085 NEW_IMAGECONST (cfg, iargs [0], image);
4086 NEW_ICONST (cfg, iargs [1], n);
4087 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4088 NEW_TEMPLOAD (cfg, res, temp);
4089 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4090 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4091 MONO_ADD_INS (bblock, store);
4092 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4094 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method (image, read32 (ip + 6), NULL)) &&
4095 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4096 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
4097 MonoClass *tclass = mono_class_from_mono_type (handle);
4098 mono_class_init (tclass);
4099 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4100 ins->type = STACK_OBJ;
4101 ins->klass = cmethod->klass;
4104 NEW_PCONST (cfg, ins, handle);
4105 ins->type = STACK_VTYPE;
4106 ins->klass = handle_class;
4122 case CEE_ADD_OVF_UN:
4124 case CEE_MUL_OVF_UN:
4126 case CEE_SUB_OVF_UN:
4131 case CEE_ENDFINALLY:
4132 /* FIXME: check stack state */
4133 MONO_INST_NEW (cfg, ins, *ip);
4134 MONO_ADD_INS (bblock, ins);
4135 ins->cil_code = ip++;
4136 start_new_bblock = 1;
4141 if (*ip == CEE_LEAVE) {
4142 target = ip + 5 + (gint32)read32(ip + 1);
4144 target = ip + 2 + (signed char)(ip [1]);
4147 /* empty the stack */
4148 while (sp != stack_start) {
4149 MONO_INST_NEW (cfg, ins, CEE_POP);
4153 MONO_ADD_INS (bblock, ins);
4156 /* fixme: call fault handler ? */
4158 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
4160 for (tmp = handlers; tmp; tmp = tmp->next) {
4162 link_bblock (cfg, bblock, tblock);
4163 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
4165 ins->inst_target_bb = tblock;
4166 MONO_ADD_INS (bblock, ins);
4168 g_list_free (handlers);
4171 MONO_INST_NEW (cfg, ins, CEE_BR);
4173 MONO_ADD_INS (bblock, ins);
4174 GET_BBLOCK (cfg, bbhash, tblock, target);
4175 link_bblock (cfg, bblock, tblock);
4176 CHECK_BBLOCK (target, ip, tblock);
4177 ins->inst_target_bb = tblock;
4178 start_new_bblock = 1;
4180 if (*ip == CEE_LEAVE)
4189 MONO_INST_NEW (cfg, ins, *ip);
4191 handle_loaded_temps (cfg, bblock, stack_start, sp);
4192 MONO_ADD_INS (bblock, ins);
4193 ins->cil_code = ip++;
4194 ins->inst_i0 = sp [0];
4195 ins->inst_i1 = sp [1];
4203 /* trampoline mono specific opcodes */
4204 case MONO_CUSTOM_PREFIX: {
4206 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
4210 case CEE_MONO_FUNC1: {
4212 gpointer func = NULL;
4217 case MONO_MARSHAL_CONV_STR_LPWSTR:
4218 func = mono_string_to_utf16;
4220 case MONO_MARSHAL_CONV_LPWSTR_STR:
4221 func = mono_string_from_utf16;
4223 case MONO_MARSHAL_CONV_LPSTR_STR:
4224 func = mono_string_new_wrapper;
4226 case MONO_MARSHAL_CONV_STR_LPTSTR:
4227 case MONO_MARSHAL_CONV_STR_LPSTR:
4228 func = mono_string_to_utf8;
4230 case MONO_MARSHAL_CONV_STR_BSTR:
4231 func = mono_string_to_bstr;
4233 case MONO_MARSHAL_CONV_STR_TBSTR:
4234 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
4235 func = mono_string_to_ansibstr;
4237 case MONO_MARSHAL_CONV_SB_LPSTR:
4238 func = mono_string_builder_to_utf8;
4240 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
4241 func = mono_array_to_savearray;
4243 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
4244 func = mono_array_to_lparray;
4246 case MONO_MARSHAL_CONV_DEL_FTN:
4247 func = mono_delegate_to_ftnptr;
4249 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
4250 func = mono_marshal_string_array;
4253 g_warning ("unknown conversion %d\n", ip [2]);
4254 g_assert_not_reached ();
4257 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4258 NEW_TEMPLOAD (cfg, *sp, temp);
4262 inline_costs += 10 * num_calls++;
4265 case CEE_MONO_PROC2: {
4266 gpointer func = NULL;
4271 case MONO_MARSHAL_CONV_LPSTR_SB:
4272 func = mono_string_utf8_to_builder;
4274 case MONO_MARSHAL_FREE_ARRAY:
4275 func = mono_marshal_free_array;
4278 g_assert_not_reached ();
4281 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4283 inline_costs += 10 * num_calls++;
4286 case CEE_MONO_PROC3: {
4287 gpointer func = NULL;
4292 case MONO_MARSHAL_CONV_STR_BYVALSTR:
4293 func = mono_string_to_byvalstr;
4295 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
4296 func = mono_string_to_byvalwstr;
4299 g_assert_not_reached ();
4302 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4304 inline_costs += 10 * num_calls++;
4310 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
4312 inline_costs += 10 * num_calls++;
4314 case CEE_MONO_LDPTR:
4315 CHECK_STACK_OVF (1);
4316 token = read32 (ip + 2);
4317 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
4321 inline_costs += 10 * num_calls++;
4323 case CEE_MONO_VTADDR:
4326 MONO_INST_NEW (cfg, ins, OP_VTADDR);
4328 ins->type = STACK_MP;
4329 ins->inst_left = *sp;
4333 case CEE_MONO_NEWOBJ: {
4334 MonoInst *iargs [2];
4336 CHECK_STACK_OVF (1);
4337 token = read32 (ip + 2);
4338 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4339 mono_class_init (klass);
4340 NEW_DOMAINCONST (cfg, iargs [0]);
4341 NEW_CLASSCONST (cfg, iargs [1], klass);
4342 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4343 NEW_TEMPLOAD (cfg, *sp, temp);
4346 inline_costs += 10 * num_calls++;
4349 case CEE_MONO_OBJADDR:
4352 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
4354 ins->type = STACK_MP;
4355 ins->inst_left = *sp;
4359 case CEE_MONO_LDNATIVEOBJ:
4361 token = read32 (ip + 2);
4362 klass = mono_method_get_wrapper_data (method, token);
4363 g_assert (klass->valuetype);
4364 mono_class_init (klass);
4365 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
4369 case CEE_MONO_RETOBJ:
4370 g_assert (cfg->ret);
4371 g_assert (method->signature->pinvoke);
4375 token = read32 (ip + 2);
4376 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4378 NEW_RETLOADA (cfg, ins);
4379 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
4381 if (sp != stack_start)
4384 MONO_INST_NEW (cfg, ins, CEE_BR);
4386 ins->inst_target_bb = end_bblock;
4387 MONO_ADD_INS (bblock, ins);
4388 link_bblock (cfg, bblock, end_bblock);
4389 start_new_bblock = 1;
4393 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
4401 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4410 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
4411 MONO_INST_NEW (cfg, ins, cmp->opcode);
4413 cmp->inst_i0 = sp [0];
4414 cmp->inst_i1 = sp [1];
4418 cmp->opcode = OP_COMPARE;
4420 ins->type = STACK_I4;
4430 CHECK_STACK_OVF (1);
4431 n = read32 (ip + 2);
4432 if (method->wrapper_type != MONO_WRAPPER_NONE)
4433 cmethod = mono_method_get_wrapper_data (method, n);
4435 cmethod = mono_get_method (image, n, NULL);
4438 * We can't do this in mono_ldftn, since it is used in
4439 * the synchronized wrapper, leading to an infinite loop.
4441 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
4442 cmethod = mono_marshal_get_synchronized_wrapper (cmethod);
4445 mono_class_init (cmethod->klass);
4446 handle_loaded_temps (cfg, bblock, stack_start, sp);
4448 NEW_METHODCONST (cfg, argconst, cmethod);
4449 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
4450 NEW_TEMPLOAD (cfg, *sp, temp);
4454 inline_costs += 10 * num_calls++;
4457 case CEE_LDVIRTFTN: {
4462 n = read32 (ip + 2);
4463 if (method->wrapper_type != MONO_WRAPPER_NONE)
4464 cmethod = mono_method_get_wrapper_data (method, n);
4466 cmethod = mono_get_method (image, n, NULL);
4468 mono_class_init (cmethod->klass);
4469 handle_loaded_temps (cfg, bblock, stack_start, sp);
4473 NEW_METHODCONST (cfg, args [1], cmethod);
4474 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
4475 NEW_TEMPLOAD (cfg, *sp, temp);
4479 inline_costs += 10 * num_calls++;
4483 CHECK_STACK_OVF (1);
4484 NEW_ARGLOAD (cfg, ins, read16 (ip + 2));
4490 CHECK_STACK_OVF (1);
4491 NEW_ARGLOADA (cfg, ins, read16 (ip + 2));
4499 handle_loaded_temps (cfg, bblock, stack_start, sp);
4500 n = read16 (ip + 2);
4501 NEW_ARGSTORE (cfg, ins, n, *sp);
4503 if (ins->opcode == CEE_STOBJ) {
4504 NEW_ARGLOADA (cfg, ins, n);
4505 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4507 MONO_ADD_INS (bblock, ins);
4511 CHECK_STACK_OVF (1);
4512 NEW_LOCLOAD (cfg, ins, read16 (ip + 2));
4518 CHECK_STACK_OVF (1);
4519 NEW_LOCLOADA (cfg, ins, read16 (ip + 2));
4527 n = read16 (ip + 2);
4528 handle_loaded_temps (cfg, bblock, stack_start, sp);
4529 NEW_LOCSTORE (cfg, ins, n, *sp);
4531 if (ins->opcode == CEE_STOBJ) {
4532 NEW_LOCLOADA (cfg, ins, n);
4533 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4535 MONO_ADD_INS (bblock, ins);
4542 if (sp != stack_start)
4544 MONO_INST_NEW (cfg, ins, 256 + ip [1]);
4545 ins->inst_left = *sp;
4548 if (header->init_locals)
4549 ins->flags |= MONO_INST_INIT;
4553 /* FIXME: set init flag if locals init is set in this method */
4555 case CEE_ENDFILTER: {
4556 MonoExceptionClause *clause, *nearest;
4557 int cc, nearest_num;
4561 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
4563 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
4564 ins->inst_left = *sp;
4566 MONO_ADD_INS (bblock, ins);
4567 start_new_bblock = 1;
4571 for (cc = 0; cc < header->num_clauses; ++cc) {
4572 clause = &header->clauses [cc];
4573 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
4574 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
4580 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
4584 case CEE_UNALIGNED_:
4585 ins_flag |= MONO_INST_UNALIGNED;
4589 ins_flag |= MONO_INST_VOLATILE;
4593 ins_flag |= MONO_INST_TAILCALL;
4599 token = read32 (ip + 2);
4600 if (method->wrapper_type != MONO_WRAPPER_NONE)
4601 klass = mono_method_get_wrapper_data (method, token);
4603 klass = mono_class_get (image, token);
4604 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
4610 MonoInst *iargs [3];
4616 handle_loaded_temps (cfg, bblock, stack_start, sp);
4617 if (ip [1] == CEE_CPBLK) {
4618 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4620 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
4628 /* FIXME: check we are in a catch handler */
4629 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
4630 load->cil_code = ip;
4631 MONO_INST_NEW (cfg, ins, CEE_THROW);
4632 ins->inst_left = load;
4634 MONO_ADD_INS (bblock, ins);
4636 start_new_bblock = 1;
4641 CHECK_STACK_OVF (1);
4642 token = read32 (ip + 2);
4643 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
4644 MonoType *type = mono_type_create_from_typespec (image, token);
4645 token = mono_type_size (type, &align);
4646 mono_metadata_free_type (type);
4648 MonoClass *szclass = mono_class_get (image, token);
4649 mono_class_init (szclass);
4650 token = mono_class_value_size (szclass, &align);
4652 NEW_ICONST (cfg, ins, token);
4657 case CEE_REFANYTYPE:
4658 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4661 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4666 g_error ("opcode 0x%02x not handled", *ip);
4669 if (start_new_bblock != 1)
4672 bblock->cil_length = ip - bblock->cil_code;
4673 bblock->next_bb = end_bblock;
4674 link_bblock (cfg, bblock, end_bblock);
4676 if (cfg->method == method && cfg->domainvar) {
4680 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
4681 call->signature = helper_sig_domain_get;
4682 call->inst.type = STACK_PTR;
4683 call->fptr = mono_domain_get;
4684 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
4686 MONO_ADD_INS (init_localsbb, store);
4689 if (header->init_locals) {
4691 for (i = 0; i < header->num_locals; ++i) {
4692 int t = header->locals [i]->type;
4693 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
4694 t = header->locals [i]->data.klass->enum_basetype->type;
4695 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
4696 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
4697 NEW_ICONST (cfg, ins, 0);
4698 NEW_LOCSTORE (cfg, store, i, ins);
4699 MONO_ADD_INS (init_localsbb, store);
4700 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
4701 MONO_INST_NEW (cfg, ins, OP_I8CONST);
4702 ins->type = STACK_I8;
4704 NEW_LOCSTORE (cfg, store, i, ins);
4705 MONO_ADD_INS (init_localsbb, store);
4706 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
4707 MONO_INST_NEW (cfg, ins, OP_R8CONST);
4708 ins->type = STACK_R8;
4709 ins->inst_p0 = (void*)&r8_0;
4710 NEW_LOCSTORE (cfg, store, i, ins);
4711 MONO_ADD_INS (init_localsbb, store);
4712 } else if (t == MONO_TYPE_VALUETYPE) {
4713 NEW_LOCLOADA (cfg, ins, i);
4714 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
4717 NEW_PCONST (cfg, ins, NULL);
4718 NEW_LOCSTORE (cfg, store, i, ins);
4719 MONO_ADD_INS (init_localsbb, store);
4725 /* resolve backward branches in the middle of an existing basic block */
4726 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
4728 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
4729 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
4730 if (tblock != start_bblock) {
4732 split_bblock (cfg, tblock, bblock);
4733 l = bblock->cil_code - header->code;
4734 bblock->cil_length = tblock->cil_length - l;
4735 tblock->cil_length = l;
4737 g_print ("recheck failed.\n");
4741 /* we compute regions here, because the length of filter clauses is not known in advance.
4742 * It is computed in the CEE_ENDFILTER case in the above switch statement*/
4743 if (cfg->method == method) {
4745 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4746 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
4747 if (cfg->verbose_level > 2)
4748 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
4751 g_hash_table_destroy (bbhash);
4754 dont_inline = g_list_remove (dont_inline, method);
4755 return inline_costs;
4758 if (cfg->method != method)
4759 g_hash_table_destroy (bbhash);
4760 dont_inline = g_list_remove (dont_inline, method);
4764 if (cfg->method != method)
4765 g_hash_table_destroy (bbhash);
4766 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
4767 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
4768 dont_inline = g_list_remove (dont_inline, method);
4773 mono_print_tree (MonoInst *tree) {
4779 arity = mono_burg_arity [tree->opcode];
4781 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
4783 switch (tree->opcode) {
4785 printf ("[%d]", tree->inst_c0);
4788 printf ("[%lld]", tree->inst_l);
4791 printf ("[%f]", *(double*)tree->inst_p0);
4794 printf ("[%f]", *(float*)tree->inst_p0);
4798 printf ("[%d]", tree->inst_c0);
4801 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
4804 printf ("[%s]", mono_arch_regname (tree->dreg));
4807 printf ("[%s]", tree->inst_newa_class->name);
4808 mono_print_tree (tree->inst_newa_len);
4819 case OP_VOIDCALLVIRT: {
4820 MonoCallInst *call = (MonoCallInst*)tree;
4822 printf ("[%s]", call->method->name);
4827 printf ("[%d (", tree->inst_c0);
4828 for (i = 0; i < tree->inst_phi_args [0]; i++) {
4831 printf ("%d", tree->inst_phi_args [i + 1]);
4843 printf ("[B%d]", tree->inst_target_bb->block_num);
4853 case OP_VOIDCALL_REG:
4854 mono_print_tree (tree->inst_left);
4866 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
4867 mono_print_tree (tree->inst_left);
4871 mono_print_tree (tree->inst_left);
4873 mono_print_tree (tree->inst_right);
4883 create_helper_signature (void)
4885 /* FIXME: set call conv */
4886 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
4887 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4888 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
4889 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
4890 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
4891 helper_sig_newarr->pinvoke = 1;
4893 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
4894 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4895 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
4896 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
4897 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
4898 helper_sig_newarr_specific->pinvoke = 1;
4900 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
4901 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4902 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
4903 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
4904 helper_sig_object_new->pinvoke = 1;
4906 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
4907 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4908 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
4909 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
4910 helper_sig_object_new_specific->pinvoke = 1;
4912 /* void* mono_method_compile (MonoMethod*) */
4913 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4914 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
4915 helper_sig_compile->pinvoke = 1;
4917 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
4918 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4919 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
4920 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
4921 helper_sig_compile_virt->pinvoke = 1;
4923 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
4924 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4925 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
4926 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
4927 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
4928 helper_sig_ldstr->pinvoke = 1;
4930 /* MonoDomain *mono_domain_get (void) */
4931 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
4932 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
4933 helper_sig_domain_get->pinvoke = 1;
4935 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
4936 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4937 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
4938 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
4939 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
4940 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
4941 helper_sig_stelem_ref->pinvoke = 1;
4943 /* long amethod (long, long) */
4944 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4945 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
4946 &mono_defaults.int64_class->byval_arg;
4947 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
4948 helper_sig_long_long_long->pinvoke = 1;
4950 /* object amethod (intptr) */
4951 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4952 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4953 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
4954 helper_sig_obj_ptr->pinvoke = 1;
4956 /* void amethod (intptr) */
4957 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4958 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4959 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
4960 helper_sig_void_ptr->pinvoke = 1;
4962 /* void amethod (MonoObject *obj) */
4963 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4964 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
4965 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
4966 helper_sig_void_obj->pinvoke = 1;
4968 /* intptr amethod (void) */
4969 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
4970 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
4971 helper_sig_ptr_void->pinvoke = 1;
4973 /* void amethod (intptr, intptr) */
4974 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4975 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4976 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
4977 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
4978 helper_sig_void_ptr_ptr->pinvoke = 1;
4980 /* void amethod (intptr, intptr, intptr) */
4981 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4982 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4983 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
4984 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
4985 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
4986 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
4988 /* intptr amethod (intptr, intptr) */
4989 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4990 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4991 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
4992 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
4993 helper_sig_ptr_ptr_ptr->pinvoke = 1;
4995 /* IntPtr amethod (object) */
4996 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4997 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
4998 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
4999 helper_sig_ptr_obj->pinvoke = 1;
5001 /* IntPtr amethod (int) */
5002 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5003 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
5004 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
5005 helper_sig_ptr_int->pinvoke = 1;
5007 /* long amethod (long, guint32) */
5008 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5009 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
5010 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
5011 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
5012 helper_sig_long_long_int->pinvoke = 1;
5014 /* ulong amethod (double) */
5015 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5016 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
5017 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
5018 helper_sig_ulong_double->pinvoke = 1;
5020 /* long amethod (double) */
5021 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5022 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
5023 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
5024 helper_sig_long_double->pinvoke = 1;
5026 /* uint amethod (double) */
5027 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5028 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
5029 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
5030 helper_sig_uint_double->pinvoke = 1;
5032 /* int amethod (double) */
5033 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5034 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
5035 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
5036 helper_sig_int_double->pinvoke = 1;
5038 /* void initobj (intptr, int size) */
5039 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5040 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
5041 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
5042 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
5043 helper_sig_initobj->pinvoke = 1;
5045 /* void memcpy (intptr, intptr, int size) */
5046 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5047 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
5048 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
5049 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
5050 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
5051 helper_sig_memcpy->pinvoke = 1;
5053 /* void memset (intptr, int val, int size) */
5054 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5055 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
5056 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
5057 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
5058 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
5059 helper_sig_memset->pinvoke = 1;
5062 static GHashTable *jit_icall_hash_name = NULL;
5063 static GHashTable *jit_icall_hash_addr = NULL;
5066 mono_find_jit_icall_by_name (const char *name)
5068 g_assert (jit_icall_hash_name);
5070 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
5071 return g_hash_table_lookup (jit_icall_hash_name, name);
5075 mono_find_jit_icall_by_addr (gconstpointer addr)
5077 g_assert (jit_icall_hash_addr);
5079 return g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
5083 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
5086 MonoMethod *wrapper;
5088 if (callinfo->wrapper)
5089 return callinfo->wrapper;
5090 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
5091 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
5092 callinfo->wrapper = mono_jit_compile_method (wrapper);
5094 return callinfo->wrapper;
5098 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
5100 MonoJitICallInfo *info;
5105 if (!jit_icall_hash_name) {
5106 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
5107 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
5110 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
5111 g_warning ("jit icall already defined \"%s\"\n", name);
5112 g_assert_not_reached ();
5115 info = g_new (MonoJitICallInfo, 1);
5122 #ifdef MONO_USE_EXC_TABLES
5123 || mono_arch_has_unwind_info (func)
5126 info->wrapper = func;
5128 info->wrapper = NULL;
5129 mono_icall_get_wrapper (info);
5132 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
5133 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
5134 if (func != info->wrapper)
5135 g_hash_table_insert (jit_icall_hash_addr, (gpointer)info->wrapper, info);
5140 static GHashTable *emul_opcode_hash = NULL;
5142 static MonoJitICallInfo *
5143 mono_find_jit_opcode_emulation (int opcode)
5145 if (emul_opcode_hash)
5146 return g_hash_table_lookup (emul_opcode_hash, (gpointer)opcode);
5152 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func)
5154 MonoJitICallInfo *info;
5156 if (!emul_opcode_hash)
5157 emul_opcode_hash = g_hash_table_new (NULL, NULL);
5159 g_assert (!sig->hasthis);
5160 g_assert (sig->param_count < 3);
5162 info = mono_register_jit_icall (func, name, sig, FALSE);
5164 g_hash_table_insert (emul_opcode_hash, (gpointer)opcode, info);
5168 decompose_foreach (MonoInst *tree, gpointer data)
5170 static MonoJitICallInfo *newarr_info = NULL;
5171 static MonoJitICallInfo *newarr_specific_info = NULL;
5172 MonoJitICallInfo *info;
5174 switch (tree->opcode) {
5176 MonoCompile *cfg = data;
5177 MonoInst *iargs [3];
5180 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
5181 g_assert (newarr_info);
5182 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
5183 g_assert (newarr_specific_info);
5186 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
5187 NEW_DOMAINCONST (cfg, iargs [0]);
5188 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
5189 iargs [2] = tree->inst_newa_len;
5194 MonoVTable *vtable = mono_class_vtable (cfg->domain,
5195 mono_array_class_get (&tree->inst_newa_class->byval_arg, 1));
5197 NEW_PCONST (cfg, iargs [0], vtable);
5198 iargs [1] = tree->inst_newa_len;
5200 info = newarr_specific_info;
5203 mono_emulate_opcode (cfg, tree, iargs, info);
5213 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
5215 switch (mono_burg_arity [tree->opcode]) {
5218 mono_inst_foreach (tree->inst_left, func, data);
5221 mono_inst_foreach (tree->inst_left, func, data);
5222 mono_inst_foreach (tree->inst_right, func, data);
5225 g_assert_not_reached ();
5232 mono_print_bb_code (MonoBasicBlock *bb) {
5234 MonoInst *c = bb->code;
5236 mono_print_tree (c);
5245 print_dfn (MonoCompile *cfg) {
5250 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
5252 for (i = 0; i < cfg->num_bblocks; ++i) {
5253 bb = cfg->bblocks [i];
5255 char* code1, *code2;
5256 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
5257 if (bb->last_ins->cil_code)
5258 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
5260 code2 = g_strdup ("");
5262 code1 [strlen (code1) - 1] = 0;
5263 code = g_strdup_printf ("%s -> %s", code1, code2);
5267 code = g_strdup ("\n");
5268 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
5270 MonoInst *c = bb->code;
5272 mono_print_tree (c);
5280 g_print ("\tprev:");
5281 for (j = 0; j < bb->in_count; ++j) {
5282 g_print (" BB%d", bb->in_bb [j]->block_num);
5284 g_print ("\t\tsucc:");
5285 for (j = 0; j < bb->out_count; ++j) {
5286 g_print (" BB%d", bb->out_bb [j]->block_num);
5288 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
5291 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
5294 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
5296 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
5304 * returns the offset used by spillvar. It allocates a new
5305 * spill variable if necessary.
5308 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
5310 MonoSpillInfo **si, *info;
5313 si = &cfg->spill_info;
5315 while (i <= spillvar) {
5318 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
5320 cfg->stack_offset -= sizeof (gpointer);
5321 info->offset = cfg->stack_offset;
5325 return (*si)->offset;
5331 g_assert_not_reached ();
5336 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
5340 g_assert (bb->code);
5341 bb->last_ins->next = inst;
5342 bb->last_ins = inst;
5344 bb->last_ins = bb->code = inst;
5349 mono_destroy_compile (MonoCompile *cfg)
5351 //mono_mempool_stats (cfg->mempool);
5352 g_hash_table_destroy (cfg->bb_hash);
5354 mono_regstate_free (cfg->rs);
5355 mono_mempool_destroy (cfg->mempool);
5356 g_list_free (cfg->ldstr_list);
5358 g_free (cfg->varinfo);
5364 mono_get_lmf_addr (void)
5366 MonoJitTlsData *jit_tls;
5368 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
5369 return &jit_tls->lmf;
5371 g_assert_not_reached ();
5376 * mono_thread_abort:
5377 * @obj: exception object
5379 * abort the thread, print exception information and stack trace
5382 mono_thread_abort (MonoObject *obj)
5384 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
5392 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
5394 MonoJitTlsData *jit_tls;
5397 jit_tls = g_new0 (MonoJitTlsData, 1);
5399 TlsSetValue (mono_jit_tls_id, jit_tls);
5401 jit_tls->abort_func = mono_thread_abort;
5402 jit_tls->end_of_stack = stack_start;
5404 lmf = g_new0 (MonoLMF, 1);
5410 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
5413 mono_thread_abort_dummy (MonoObject *obj)
5415 if (mono_thread_attach_aborted_cb)
5416 mono_thread_attach_aborted_cb (obj);
5418 mono_thread_abort (obj);
5422 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
5424 MonoJitTlsData *jit_tls;
5427 jit_tls = g_new0 (MonoJitTlsData, 1);
5429 TlsSetValue (mono_jit_tls_id, jit_tls);
5431 jit_tls->abort_func = mono_thread_abort_dummy;
5432 jit_tls->end_of_stack = stack_start;
5434 lmf = g_new0 (MonoLMF, 1);
5441 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
5443 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
5447 ji->data.target = target;
5448 ji->next = cfg->patch_info;
5450 cfg->patch_info = ji;
5454 mono_remove_patch_info (MonoCompile *cfg, int ip)
5456 MonoJumpInfo **ji = &cfg->patch_info;
5459 if ((*ji)->ip.i == ip)
5462 ji = &((*ji)->next);
5467 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
5468 MonoJitICallInfo *info;
5470 switch (mono_burg_arity [tree->opcode]) {
5473 dec_foreach (tree->inst_left, cfg);
5475 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5476 MonoInst *iargs [2];
5478 iargs [0] = tree->inst_left;
5480 mono_emulate_opcode (cfg, tree, iargs, info);
5486 if (tree->opcode == OP_LMUL
5487 && (cfg->opt & MONO_OPT_INTRINS)
5488 && (tree->inst_left->opcode == CEE_CONV_I8
5489 || tree->inst_left->opcode == CEE_CONV_U8)
5490 && tree->inst_left->inst_left->type == STACK_I4
5491 && (tree->inst_right->opcode == CEE_CONV_I8
5492 || tree->inst_right->opcode == CEE_CONV_U8)
5493 && tree->inst_right->inst_left->type == STACK_I4) {
5494 tree->opcode = OP_BIGMUL;
5495 tree->inst_left = tree->inst_left->inst_left;
5496 tree->inst_right = tree->inst_right->inst_left;
5497 dec_foreach (tree, cfg);
5498 } else if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5499 MonoInst *iargs [2];
5501 iargs [0] = tree->inst_i0;
5502 iargs [1] = tree->inst_i1;
5504 mono_emulate_opcode (cfg, tree, iargs, info);
5506 dec_foreach (iargs [0], cfg);
5507 dec_foreach (iargs [1], cfg);
5510 dec_foreach (tree->inst_left, cfg);
5511 dec_foreach (tree->inst_right, cfg);
5515 g_assert_not_reached ();
5517 decompose_foreach (tree, cfg);
5521 decompose_pass (MonoCompile *cfg) {
5524 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5527 cfg->prev_ins = NULL;
5528 for (tree = cfg->cbb->code; tree; tree = tree->next) {
5529 dec_foreach (tree, cfg);
5530 cfg->prev_ins = tree;
5536 nullify_basic_block (MonoBasicBlock *bb)
5543 bb->code = bb->last_ins = NULL;
5547 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
5551 for (i = 0; i < bb->out_count; i++) {
5552 MonoBasicBlock *ob = bb->out_bb [i];
5553 for (j = 0; j < ob->in_count; j++) {
5554 if (ob->in_bb [j] == orig)
5555 ob->in_bb [j] = repl;
5562 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
5564 bb->out_count = bbn->out_count;
5565 bb->out_bb = bbn->out_bb;
5567 replace_basic_block (bb, bbn, bb);
5571 bb->last_ins->next = bbn->code;
5572 bb->last_ins = bbn->last_ins;
5575 bb->code = bbn->code;
5576 bb->last_ins = bbn->last_ins;
5578 bb->next_bb = bbn->next_bb;
5579 nullify_basic_block (bbn);
5583 optimize_branches (MonoCompile *cfg) {
5584 int changed = FALSE;
5585 MonoBasicBlock *bb, *bbn;
5590 /* we skip the entry block (exit is handled specially instead ) */
5591 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
5593 /* dont touch code inside exception clauses */
5594 if (bb->region != -1)
5597 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
5598 if (cfg->verbose_level > 2)
5599 g_print ("nullify block triggered %d\n", bbn->block_num);
5601 bb->next_bb = bbn->next_bb;
5602 nullify_basic_block (bbn);
5606 if (bb->out_count == 1) {
5607 bbn = bb->out_bb [0];
5609 if (bb->region == bbn->region && bb->next_bb == bbn) {
5610 /* the block are in sequence anyway ... */
5613 * miguel: I do not understand what the test below does, could we
5614 * use a macro, or a comment here? opcode > CEE_BEQ && <= BLT_UN
5616 * It could also test for bb->last_in only once, and the value
5617 * could be cached (last_ins->opcode)
5619 if (bb->last_ins && (bb->last_ins->opcode == CEE_BR || (
5620 (bb->last_ins && bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN)))) {
5621 bb->last_ins->opcode = CEE_NOP;
5623 if (cfg->verbose_level > 2)
5624 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
5626 if (bbn->in_count == 1) {
5628 if (bbn != cfg->bb_exit) {
5629 if (cfg->verbose_level > 2)
5630 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
5631 merge_basic_blocks (bb, bbn);
5635 //mono_print_bb_code (bb);
5645 /* we skip the entry block (exit is handled specially instead ) */
5646 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
5648 /* dont touch code inside exception clauses */
5649 if (bb->region != -1)
5652 if (bb->out_count == 1) {
5653 bbn = bb->out_bb [0];
5655 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
5656 bbn = bb->last_ins->inst_target_bb;
5657 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5658 bbn->code->inst_target_bb->region == bb->region) {
5660 if (cfg->verbose_level > 2)
5661 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
5662 bb->block_num, bbn->block_num);
5664 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
5665 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
5670 } else if (bb->out_count == 2) {
5671 /* fixme: this does not correctly - no idea whats wrong */
5672 if (0 && bb->last_ins && bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
5673 bbn = bb->last_ins->inst_true_bb;
5674 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5675 bbn->code->inst_target_bb->region == bb->region) {
5676 if (cfg->verbose_level > 2)
5677 g_print ("cbranch to branch triggered %d -> %d (0x%02x)\n", bb->block_num,
5678 bbn->block_num, bbn->code->opcode);
5680 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
5681 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
5685 bbn = bb->last_ins->inst_false_bb;
5686 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5687 bbn->code->inst_target_bb->region == bb->region) {
5688 if (cfg->verbose_level > 2)
5689 g_print ("cbranch to branch triggered %d -> %d (0x%02x)\n", bb->block_num,
5690 bbn->block_num, bbn->code->opcode);
5692 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
5693 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
5705 mono_compile_create_vars (MonoCompile *cfg)
5707 MonoMethodSignature *sig;
5708 MonoMethodHeader *header;
5711 header = ((MonoMethodNormal *)cfg->method)->header;
5713 sig = cfg->method->signature;
5715 if (!MONO_TYPE_IS_VOID (sig->ret)) {
5716 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
5717 cfg->ret->opcode = OP_RETARG;
5718 cfg->ret->inst_vtype = sig->ret;
5719 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
5723 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
5725 for (i = 0; i < sig->param_count; ++i)
5726 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
5728 cfg->locals_start = cfg->num_varinfo;
5730 for (i = 0; i < header->num_locals; ++i)
5731 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
5736 mono_print_code (MonoCompile *cfg)
5740 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5741 MonoInst *tree = bb->code;
5746 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
5748 for (; tree; tree = tree->next) {
5749 mono_print_tree (tree);
5754 bb->last_ins->next = NULL;
5759 extern const char * const mono_burg_rule_string [];
5762 emit_state (MonoCompile *cfg, MBState *state, int goal)
5765 int ern = mono_burg_rule (state, goal);
5766 const guint16 *nts = mono_burg_nts [ern];
5769 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
5773 // state->reg1 = state->reg2; /* chain rule */
5775 state->reg1 = mono_regstate_next_int (cfg->rs);
5776 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
5779 state->reg1 = mono_regstate_next_int (cfg->rs);
5780 state->reg2 = mono_regstate_next_int (cfg->rs);
5783 state->reg1 = mono_regstate_next_float (cfg->rs);
5790 mono_burg_kids (state, ern, kids);
5792 emit_state (cfg, kids [0], nts [0]);
5794 emit_state (cfg, kids [1], nts [1]);
5796 g_assert (!nts [3]);
5797 emit_state (cfg, kids [2], nts [2]);
5802 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
5803 if ((emit = mono_burg_func [ern]))
5804 emit (state, state->tree, cfg);
5807 #define DEBUG_SELECTION
5810 mini_select_instructions (MonoCompile *cfg)
5814 cfg->state_pool = mono_mempool_new ();
5815 cfg->rs = mono_regstate_new ();
5817 #ifdef DEBUG_SELECTION
5818 if (cfg->verbose_level >= 4) {
5819 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5820 MonoInst *tree = bb->code;
5821 g_print ("DUMP BLOCK %d:\n", bb->block_num);
5824 for (; tree; tree = tree->next) {
5825 mono_print_tree (tree);
5832 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5833 MonoInst *tree = bb->code, *next;
5839 bb->last_ins = NULL;
5842 mono_regstate_reset (cfg->rs);
5844 #ifdef DEBUG_SELECTION
5845 if (cfg->verbose_level >= 3)
5846 g_print ("LABEL BLOCK %d:\n", bb->block_num);
5848 for (; tree; tree = next) {
5850 #ifdef DEBUG_SELECTION
5851 if (cfg->verbose_level >= 3) {
5852 mono_print_tree (tree);
5857 if (!(mbstate = mono_burg_label (tree, cfg))) {
5858 g_warning ("unabled to label tree %p", tree);
5859 mono_print_tree (tree);
5861 g_assert_not_reached ();
5863 emit_state (cfg, mbstate, MB_NTERM_stmt);
5865 bb->max_ireg = cfg->rs->next_vireg;
5866 bb->max_freg = cfg->rs->next_vfreg;
5869 bb->last_ins->next = NULL;
5871 mono_mempool_empty (cfg->state_pool);
5873 mono_mempool_destroy (cfg->state_pool);
5877 mono_codegen (MonoCompile *cfg)
5879 MonoJumpInfo *patch_info;
5881 int i, max_epilog_size;
5884 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5885 cfg->spill_count = 0;
5886 /* we reuse dfn here */
5887 /* bb->dfn = bb_count++; */
5888 mono_arch_local_regalloc (cfg, bb);
5891 if (mono_trace_coverage)
5892 mono_allocate_coverage_info (cfg->method, cfg->num_bblocks);
5894 code = mono_arch_emit_prolog (cfg);
5896 if (mono_jit_profile)
5897 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
5899 cfg->code_len = code - cfg->native_code;
5900 cfg->prolog_end = cfg->code_len;
5902 mono_debug_open_method (cfg);
5904 /* emit code all basic blocks */
5905 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5906 bb->native_offset = cfg->code_len;
5907 mono_arch_output_basic_block (cfg, bb);
5909 cfg->bb_exit->native_offset = cfg->code_len;
5911 code = cfg->native_code + cfg->code_len;
5913 max_epilog_size = mono_arch_max_epilog_size (cfg);
5915 /* we always allocate code in cfg->domain->code_mp to increase locality */
5916 cfg->code_size = cfg->code_len + max_epilog_size;
5917 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
5918 code = mono_mempool_alloc (cfg->domain->code_mp, cfg->code_size);
5919 memcpy (code, cfg->native_code, cfg->code_len);
5920 g_free (cfg->native_code);
5921 cfg->native_code = code;
5922 code = cfg->native_code + cfg->code_len;
5924 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
5926 cfg->epilog_begin = cfg->code_len;
5928 if (mono_jit_profile)
5929 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
5931 cfg->code_len = code - cfg->native_code;
5933 mono_arch_emit_epilog (cfg);
5935 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5936 switch (patch_info->type) {
5937 case MONO_PATCH_INFO_ABS: {
5938 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
5940 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
5941 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
5942 patch_info->data.name = info->name;
5946 case MONO_PATCH_INFO_SWITCH: {
5947 gpointer *table = g_new (gpointer, patch_info->table_size);
5948 patch_info->ip.i = patch_info->ip.label->inst_c0;
5949 for (i = 0; i < patch_info->table_size; i++) {
5950 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
5952 patch_info->data.target = table;
5961 if (cfg->verbose_level > 1)
5962 g_print ("Method %s::%s emmitted at %p to %p\n", cfg->method->klass->name,
5963 cfg->method->name, cfg->native_code, cfg->native_code + cfg->code_len);
5965 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info);
5967 mono_debug_close_method (cfg);
5971 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
5976 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
5977 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
5979 if (cp->opcode == OP_ICONST) {
5980 if (cfg->opt & MONO_OPT_CONSPROP) {
5981 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
5985 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
5986 if (cfg->opt & MONO_OPT_COPYPROP) {
5987 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
5993 arity = mono_burg_arity [tree->opcode];
5996 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
5997 if (cfg->opt & MONO_OPT_CFOLD)
5998 mono_constant_fold_inst (tree, NULL);
6000 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
6001 if (cfg->opt & MONO_OPT_CFOLD)
6002 mono_constant_fold_inst (tree, NULL);
6004 mono_constant_fold_inst (tree, NULL);
6010 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
6014 switch (tree->opcode) {
6024 if (tree->ssa_op == MONO_SSA_NOP) {
6025 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6042 case OP_VOIDCALL_REG:
6043 case OP_VOIDCALLVIRT:
6045 MonoCallInst *call = (MonoCallInst *)tree;
6046 MonoMethodSignature *sig = call->signature;
6047 int i, byref = FALSE;
6049 for (i = 0; i < sig->param_count; i++) {
6050 if (sig->params [i]->byref) {
6057 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6065 arity = mono_burg_arity [tree->opcode];
6071 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6074 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6075 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
6078 g_assert_not_reached ();
6083 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
6085 MonoInst *tree = bb->code;
6091 for (; tree; tree = tree->next) {
6093 mono_cprop_copy_values (cfg, tree, acp);
6095 mono_cprop_invalidate_values (tree, acp, acp_size);
6097 if (tree->ssa_op == MONO_SSA_STORE &&
6098 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
6099 MonoInst *i1 = tree->inst_i1;
6101 acp [tree->inst_i0->inst_c0] = NULL;
6103 for (i = 0; i < acp_size; i++) {
6104 if (acp [i] && acp [i]->opcode != OP_ICONST &&
6105 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
6110 if (i1->opcode == OP_ICONST) {
6111 acp [tree->inst_i0->inst_c0] = i1;
6112 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
6114 if (i1->ssa_op == MONO_SSA_LOAD &&
6115 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
6116 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
6117 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
6118 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
6123 if (tree->opcode == CEE_BEQ) {
6124 g_assert (tree->inst_i0->opcode == OP_COMPARE);
6125 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
6126 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
6128 tree->opcode = CEE_BR;
6129 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
6130 tree->inst_target_bb = tree->inst_true_bb;
6132 tree->inst_target_bb = tree->inst_false_bb;
6141 mono_local_cprop (MonoCompile *cfg)
6146 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
6148 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6149 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
6150 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
6155 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int parts)
6157 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
6158 guint8 *ip = (guint8 *)header->code;
6161 int dfn = 0, i, code_size_ratio;
6163 mono_jit_stats.methods_compiled++;
6164 if (mono_jit_profile)
6165 mono_profiler_method_jit (method);
6167 cfg = g_new0 (MonoCompile, 1);
6168 cfg->method = method;
6169 cfg->mempool = mono_mempool_new ();
6171 cfg->bb_hash = g_hash_table_new (g_direct_hash, NULL);
6172 cfg->domain = domain;
6173 cfg->verbose_level = mini_verbose;
6174 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
6175 ((MonoMethodNormal *)method)->header->max_stack);
6178 * create MonoInst* which represents arguments and local variables
6180 mono_compile_create_vars (cfg);
6182 if (cfg->verbose_level > 2)
6183 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
6185 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
6186 mono_destroy_compile (cfg);
6187 if (mono_jit_profile)
6188 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
6192 mono_jit_stats.basic_blocks += cfg->num_bblocks;
6193 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
6195 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
6197 /* Depth-first ordering on basic blocks */
6198 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
6200 if (cfg->opt & MONO_OPT_BRANCH)
6201 optimize_branches (cfg);
6203 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
6204 if (cfg->num_bblocks != dfn + 1) {
6207 cfg->num_bblocks = dfn + 1;
6209 /* we always remove unreachable code, because the code in them may be
6210 * inconsistent (access to dead variables for example) */
6211 for (bb = cfg->bb_entry; bb;) {
6212 MonoBasicBlock *bbn = bb->next_bb;
6214 if (bbn && bbn->region == -1 && !bbn->dfn) {
6215 if (cfg->verbose_level > 1)
6216 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
6217 bb->next_bb = bbn->next_bb;
6218 nullify_basic_block (bbn);
6225 if (cfg->opt & MONO_OPT_LOOP) {
6226 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
6227 mono_compute_natural_loops (cfg);
6231 /* after method_to_ir */
6235 //#define DEBUGSSA "logic_run"
6236 #define DEBUGSSA_CLASS "Tests"
6240 if (!header->num_clauses && !cfg->disable_ssa) {
6241 mono_local_cprop (cfg);
6242 mono_ssa_compute (cfg);
6246 /* fixme: add all optimizations which requires SSA */
6247 if (cfg->opt & (MONO_OPT_DEADCE)) {
6248 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
6249 mono_local_cprop (cfg);
6250 mono_ssa_compute (cfg);
6252 if (cfg->verbose_level >= 2) {
6259 /* after SSA translation */
6263 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
6264 if (cfg->comp_done & MONO_COMP_SSA) {
6265 mono_ssa_cprop (cfg);
6267 mono_local_cprop (cfg);
6271 if (cfg->comp_done & MONO_COMP_SSA) {
6272 mono_ssa_deadce (cfg);
6274 //mono_ssa_strength_reduction (cfg);
6276 mono_ssa_remove (cfg);
6278 if (cfg->opt & MONO_OPT_BRANCH)
6279 optimize_branches (cfg);
6282 /* after SSA removal */
6286 decompose_pass (cfg);
6288 /* FIXME: disabled with exception clauses: bug #42136 */
6289 if ((!header->num_clauses) && (cfg->opt & MONO_OPT_LINEARS)) {
6292 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
6293 cfg->comp_done &= ~MONO_COMP_LIVENESS;
6294 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
6295 mono_analyze_liveness (cfg);
6297 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
6298 regs = mono_arch_get_global_int_regs (cfg);
6299 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
6303 //mono_print_code (cfg);
6307 /* variables are allocated after decompose, since decompose could create temps */
6308 mono_arch_allocate_vars (cfg);
6310 if (cfg->opt & MONO_OPT_CFOLD)
6311 mono_constant_fold (cfg);
6313 mini_select_instructions (cfg);
6316 if (cfg->verbose_level >= 2) {
6317 char *id = mono_method_full_name (cfg->method, FALSE);
6318 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
6322 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
6324 jinfo = g_new0 (MonoJitInfo, 1);
6325 jinfo->method = method;
6326 jinfo->code_start = cfg->native_code;
6327 jinfo->code_size = cfg->code_len;
6328 jinfo->used_regs = cfg->used_int_regs;
6330 if (header->num_clauses) {
6333 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
6334 jinfo->num_clauses = header->num_clauses;
6335 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
6336 sizeof (MonoJitExceptionInfo) * header->num_clauses);
6338 for (i = 0; i < header->num_clauses; i++) {
6339 MonoExceptionClause *ec = &header->clauses [i];
6340 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
6341 MonoBasicBlock *tblock;
6343 ei->flags = ec->flags;
6345 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
6346 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
6348 ei->data.filter = cfg->native_code + tblock->native_offset;
6350 ei->data.token = ec->token_or_filter;
6353 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
6355 ei->try_start = cfg->native_code + tblock->native_offset;
6356 g_assert (tblock->native_offset);
6357 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
6359 ei->try_end = cfg->native_code + tblock->native_offset;
6360 g_assert (tblock->native_offset);
6361 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
6363 ei->handler_start = cfg->native_code + tblock->native_offset;
6367 mono_jit_info_table_add (cfg->domain, jinfo);
6369 /* collect statistics */
6370 mono_jit_stats.allocated_code_size += cfg->code_len;
6371 code_size_ratio = cfg->code_len;
6372 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
6373 mono_jit_stats.biggest_method_size = code_size_ratio;
6374 mono_jit_stats.biggest_method = method;
6376 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
6377 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
6378 mono_jit_stats.max_code_size_ratio = code_size_ratio;
6379 mono_jit_stats.max_ratio_method = method;
6381 mono_jit_stats.native_code_size += cfg->code_len;
6383 if (mono_jit_profile)
6384 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
6390 mono_jit_compile_method (MonoMethod *method)
6392 /* FIXME: later copy the code from mono */
6393 MonoDomain *target_domain, *domain = mono_domain_get ();
6395 GHashTable *jit_code_hash;
6398 if (default_opt & MONO_OPT_SHARED)
6399 target_domain = mono_root_domain;
6401 target_domain = domain;
6403 jit_code_hash = target_domain->jit_code_hash;
6405 if ((code = g_hash_table_lookup (jit_code_hash, method))) {
6406 mono_jit_stats.methods_lookups++;
6410 #ifdef MONO_USE_AOT_COMPILER
6411 if (!mono_compile_aot) {
6412 mono_class_init (method->klass);
6413 if ((code = mono_aot_get_method (method))) {
6414 g_hash_table_insert (jit_code_hash, method, code);
6420 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
6421 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
6422 if (!method->info) {
6425 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
6426 mono_lookup_pinvoke_call (method);
6427 #ifdef MONO_USE_EXC_TABLES
6428 if (mono_method_blittable (method)) {
6429 method->info = method->addr;
6432 nm = mono_marshal_get_native_wrapper (method);
6433 method->info = mono_compile_method (nm);
6435 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
6436 //mono_debug_add_wrapper (method, nm);
6437 #ifdef MONO_USE_EXC_TABLES
6441 return method->info;
6442 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
6443 const char *name = method->name;
6446 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
6447 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
6448 /* FIXME: uhm, we need a wrapper to handle exceptions? */
6449 return (gpointer)mono_delegate_ctor;
6450 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
6451 nm = mono_marshal_get_delegate_invoke (method);
6452 return mono_jit_compile_method (nm);
6453 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
6454 nm = mono_marshal_get_delegate_begin_invoke (method);
6455 return mono_jit_compile_method (nm);
6456 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
6457 nm = mono_marshal_get_delegate_end_invoke (method);
6458 return mono_jit_compile_method (nm);
6464 cfg = mini_method_compile (method, default_opt, target_domain, 0);
6465 code = cfg->native_code;
6466 mono_destroy_compile (cfg);
6468 g_hash_table_insert (jit_code_hash, method, code);
6470 if (target_domain->jump_target_hash) {
6471 MonoJumpInfo patch_info;
6473 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
6475 patch_info.next = NULL;
6476 patch_info.ip.i = 0;
6477 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
6478 patch_info.data.method = method;
6479 g_hash_table_remove (target_domain->jump_target_hash, method);
6481 for (tmp = list; tmp; tmp = tmp->next)
6482 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info);
6483 g_slist_free (list);
6485 /* make sure runtime_init is called */
6486 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
6492 * mono_jit_runtime_invoke:
6493 * @method: the method to invoke
6494 * @obj: this pointer
6495 * @params: array of parameter values.
6496 * @exc: used to catch exceptions objects
6499 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
6502 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
6503 invoke = mono_marshal_get_runtime_invoke (method);
6504 runtime_invoke = mono_jit_compile_method (invoke);
6505 return runtime_invoke (obj, params, exc);
6508 #ifdef PLATFORM_WIN32
6509 #define GET_CONTEXT \
6510 struct sigcontext *ctx = (struct sigcontext*)_dummy;
6512 #define GET_CONTEXT \
6513 void **_p = (void **)&_dummy; \
6514 struct sigcontext *ctx = (struct sigcontext *)++_p;
6518 sigfpe_signal_handler (int _dummy)
6523 exc = mono_get_exception_divide_by_zero ();
6525 mono_arch_handle_exception (ctx, exc, FALSE);
6529 sigill_signal_handler (int _dummy)
6533 exc = mono_get_exception_execution_engine ("SIGILL");
6535 mono_arch_handle_exception (ctx, exc, FALSE);
6539 sigsegv_signal_handler (int _dummy)
6544 exc = mono_get_exception_null_reference ();
6546 mono_arch_handle_exception (ctx, exc, FALSE);
6550 sigusr1_signal_handler (int _dummy)
6555 thread = mono_thread_current ();
6557 g_assert (thread->abort_exc);
6559 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
6563 mono_runtime_install_handlers (void)
6565 #ifndef PLATFORM_WIN32
6566 struct sigaction sa;
6569 #ifdef PLATFORM_WIN32
6571 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
6572 win32_seh_set_handler(SIGILL, sigill_signal_handler);
6573 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
6574 #else /* !PLATFORM_WIN32 */
6576 /* libpthreads has its own implementation of sigaction(),
6577 * but it seems to work well with our current exception
6578 * handlers. If not we must call syscall directly instead
6582 sa.sa_handler = sigfpe_signal_handler;
6583 sigemptyset (&sa.sa_mask);
6585 //g_assert (syscall (SYS_sigaction, SIGFPE, &sa, NULL) != -1);
6586 g_assert (sigaction (SIGFPE, &sa, NULL) != -1);
6589 sa.sa_handler = sigill_signal_handler;
6590 sigemptyset (&sa.sa_mask);
6592 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
6593 g_assert (sigaction (SIGILL, &sa, NULL) != -1);
6595 /* catch thread abort signal */
6596 sa.sa_handler = sigusr1_signal_handler;
6597 sigemptyset (&sa.sa_mask);
6599 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
6600 g_assert (sigaction (mono_thread_get_abort_signal (), &sa, NULL) != -1);
6604 sa.sa_handler = sigsegv_signal_handler;
6605 sigemptyset (&sa.sa_mask);
6607 //g_assert (syscall (SYS_sigaction, SIGSEGV, &sa, NULL) != -1);
6608 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
6610 #endif /* PLATFORM_WIN32 */
6613 /* mono_jit_create_remoting_trampoline:
6614 * @method: pointer to the method info
6616 * Creates a trampoline which calls the remoting functions. This
6617 * is used in the vtable of transparent proxies.
6619 * Returns: a pointer to the newly created code
6622 mono_jit_create_remoting_trampoline (MonoMethod *method)
6625 guint8 *addr = NULL;
6627 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
6628 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
6629 nm = mono_marshal_get_remoting_invoke (method);
6630 addr = mono_compile_method (nm);
6632 addr = mono_compile_method (method);
6637 static CRITICAL_SECTION ms;
6640 mini_init (const char *filename)
6644 metadata_section = &ms;
6645 InitializeCriticalSection (metadata_section);
6647 mono_jit_tls_id = TlsAlloc ();
6648 mono_thread_start_cb (GetCurrentThreadId (), (gpointer)-1, NULL);
6652 mono_runtime_install_handlers ();
6654 mono_install_compile_method (mono_jit_compile_method);
6655 mono_install_trampoline (mono_arch_create_jit_trampoline);
6656 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
6657 mono_install_runtime_invoke (mono_jit_runtime_invoke);
6658 mono_install_handler (mono_arch_get_throw_exception ());
6659 mono_install_stack_walk (mono_jit_walk_stack);
6660 mono_install_get_config_dir ();
6662 domain = mono_init (filename);
6665 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
6666 ves_icall_get_frame_info);
6667 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
6668 ves_icall_get_trace);
6669 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
6670 mono_runtime_install_handlers);
6673 create_helper_signature ();
6675 mono_arch_register_lowlevel_calls ();
6676 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
6677 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
6679 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
6680 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
6682 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
6683 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
6684 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
6686 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
6687 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
6688 helper_sig_void_ptr, TRUE);
6691 * NOTE, NOTE, NOTE, NOTE:
6692 * when adding emulation for some opcodes, remember to also add a dummy
6693 * rule to the burg files, because we need the arity information to be correct.
6695 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult);
6696 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un);
6697 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf);
6698 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv);
6699 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un);
6700 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem);
6701 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un);
6703 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl);
6704 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr);
6705 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un);
6707 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8);
6708 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4);
6709 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8);
6710 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8);
6712 #if SIZEOF_VOID_P == 4
6713 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4);
6715 #warning "fixme: add opcode emulation"
6718 /* other jit icalls */
6719 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
6720 helper_sig_ptr_ptr_ptr, FALSE);
6721 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
6722 mono_register_jit_icall (mono_threads_get_static_data, "mono_threads_get_static_data", helper_sig_ptr_int, FALSE);
6723 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
6724 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
6725 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
6726 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
6727 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
6728 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
6729 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
6730 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
6731 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
6732 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
6733 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
6734 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
6735 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
6736 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
6737 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
6738 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
6739 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
6740 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
6741 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
6742 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
6743 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
6744 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
6745 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
6746 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
6747 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
6748 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
6749 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
6750 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
6752 mono_runtime_init (domain, mono_thread_start_cb,
6753 mono_thread_attach_cb);
6755 //mono_thread_attach (domain);
6759 MonoJitStats mono_jit_stats = {0};
6762 print_jit_stats (void)
6764 if (mono_jit_stats.enabled) {
6765 g_print ("Mono Jit statistics\n");
6766 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
6767 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
6768 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
6769 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
6770 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
6771 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
6772 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
6773 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
6774 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
6775 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
6776 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
6777 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
6778 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
6779 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
6780 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
6781 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
6782 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
6783 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
6785 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
6786 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
6787 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
6788 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
6789 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
6794 mini_cleanup (MonoDomain *domain)
6797 * mono_runtime_cleanup() and mono_domain_finalize () need to
6798 * be called early since they need the execution engine still
6799 * fully working (mono_domain_finalize may invoke managed finalizers
6800 * and mono_runtime_cleanup will wait for other threads to finish).
6802 mono_domain_finalize (domain);
6804 mono_runtime_cleanup (domain);
6806 mono_profiler_shutdown ();
6808 mono_debug_cleanup ();
6809 #ifdef PLATFORM_WIN32
6810 win32_seh_cleanup();
6813 mono_domain_unload (domain, TRUE);
6816 DeleteCriticalSection (metadata_section);
6820 mono_set_defaults (int verbose_level, guint32 opts)
6822 mini_verbose = verbose_level;