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)
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);
3775 if (!cfg->domain->thread_static_fields || !(addr = g_hash_table_lookup (cfg->domain->thread_static_fields, field))) {
3777 vtable = mono_class_vtable (cfg->domain, klass);
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 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
3852 MonoInst *iargs [2];
3853 MonoInst *load, *vtoffset, *add, *val, *vstore;
3858 token = read32 (ip + 1);
3859 if (method->wrapper_type != MONO_WRAPPER_NONE)
3860 klass = mono_method_get_wrapper_data (method, token);
3862 klass = mono_class_get (image, token);
3863 mono_class_init (klass);
3865 /* much like NEWOBJ */
3866 NEW_DOMAINCONST (cfg, iargs [0]);
3867 NEW_CLASSCONST (cfg, iargs [1], klass);
3869 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3870 NEW_TEMPLOAD (cfg, load, temp);
3871 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3872 MONO_INST_NEW (cfg, add, CEE_ADD);
3873 add->inst_left = load;
3874 add->inst_right = vtoffset;
3877 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
3878 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
3879 vstore->cil_code = ip;
3880 vstore->inst_left = add;
3881 vstore->inst_right = val;
3883 if (vstore->opcode == CEE_STOBJ) {
3884 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
3886 MONO_ADD_INS (bblock, vstore);
3888 NEW_TEMPLOAD (cfg, load, temp);
3896 MONO_INST_NEW (cfg, ins, *ip);
3900 token = read32 (ip + 1);
3902 /* allocate the domainvar - becaus this is used in decompose_foreach */
3903 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)
3904 mono_get_domainvar (cfg);
3906 if (method->wrapper_type != MONO_WRAPPER_NONE)
3907 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3909 klass = mono_class_get (image, token);
3911 mono_class_init (klass);
3912 ins->inst_newa_class = klass;
3913 ins->inst_newa_len = *sp;
3914 ins->type = STACK_OBJ;
3921 MONO_INST_NEW (cfg, ins, *ip);
3922 ins->cil_code = ip++;
3924 ins->inst_left = *sp;
3925 ins->type = STACK_PTR;
3931 klass = mono_class_get (image, read32 (ip + 1));
3932 mono_class_init (klass);
3933 NEW_LDELEMA (cfg, ins, sp, klass);
3948 case CEE_LDELEM_REF: {
3952 * ldind.x (ldelema (array, index))
3953 * ldelema does the bounds check
3957 klass = array_access_to_klass (*ip);
3958 NEW_LDELEMA (cfg, load, sp, klass);
3959 load->cil_code = ip;
3960 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
3962 ins->inst_left = load;
3964 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
3974 case CEE_STELEM_R8: {
3978 * stind.x (ldelema (array, index), val)
3979 * ldelema does the bounds check
3983 klass = array_access_to_klass (*ip);
3984 NEW_LDELEMA (cfg, load, sp, klass);
3985 load->cil_code = ip;
3986 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
3988 ins->inst_left = load;
3989 ins->inst_right = sp [2];
3991 handle_loaded_temps (cfg, bblock, stack_start, sp);
3992 MONO_ADD_INS (bblock, ins);
3993 /* FIXME: add the implicit STELEM_REF castclass */
3995 cfg->disable_ssa = TRUE;
3998 case CEE_STELEM_REF: {
3999 MonoInst *iargs [3];
4004 handle_loaded_temps (cfg, bblock, stack_start, sp);
4010 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4014 NEW_GROUP (cfg, group, sp [0], sp [1]);
4015 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4017 ins->inst_left = group;
4018 ins->inst_right = sp [2];
4019 MONO_ADD_INS (bblock, ins);
4024 cfg->disable_ssa = TRUE;
4027 case CEE_CKFINITE: {
4028 MonoInst *store, *temp;
4031 /* this instr. can throw exceptions as side effect,
4032 * so we cant eliminate dead code which contains CKFINITE opdodes.
4033 * Spilling to memory makes sure that we always perform
4037 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4039 ins->inst_left = sp [-1];
4040 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4042 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4043 store->cil_code = ip;
4044 MONO_ADD_INS (bblock, store);
4046 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4053 g_error ("opcode 0x%02x not handled", *ip);
4057 MonoClass *handle_class;
4059 CHECK_STACK_OVF (1);
4061 n = read32 (ip + 1);
4063 handle = mono_ldtoken (image, n, &handle_class);
4064 mono_class_init (handle_class);
4066 if (((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)) {
4068 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4070 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4072 NEW_IMAGECONST (cfg, iargs [0], image);
4073 NEW_ICONST (cfg, iargs [1], n);
4074 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4075 NEW_TEMPLOAD (cfg, res, temp);
4076 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4077 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4078 MONO_ADD_INS (bblock, store);
4079 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4081 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method (image, read32 (ip + 6), NULL)) &&
4082 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4083 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
4084 MonoClass *tclass = mono_class_from_mono_type (handle);
4085 mono_class_init (tclass);
4086 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4087 ins->type = STACK_OBJ;
4088 ins->klass = cmethod->klass;
4091 NEW_PCONST (cfg, ins, handle);
4092 ins->type = STACK_VTYPE;
4093 ins->klass = handle_class;
4109 case CEE_ADD_OVF_UN:
4111 case CEE_MUL_OVF_UN:
4113 case CEE_SUB_OVF_UN:
4118 case CEE_ENDFINALLY:
4119 /* FIXME: check stack state */
4120 MONO_INST_NEW (cfg, ins, *ip);
4121 MONO_ADD_INS (bblock, ins);
4122 ins->cil_code = ip++;
4123 start_new_bblock = 1;
4128 if (*ip == CEE_LEAVE) {
4129 target = ip + 5 + (gint32)read32(ip + 1);
4131 target = ip + 2 + (signed char)(ip [1]);
4134 /* empty the stack */
4135 while (sp != stack_start) {
4136 MONO_INST_NEW (cfg, ins, CEE_POP);
4140 MONO_ADD_INS (bblock, ins);
4143 /* fixme: call fault handler ? */
4145 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
4147 for (tmp = handlers; tmp; tmp = tmp->next) {
4149 link_bblock (cfg, bblock, tblock);
4150 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
4152 ins->inst_target_bb = tblock;
4153 MONO_ADD_INS (bblock, ins);
4155 g_list_free (handlers);
4158 MONO_INST_NEW (cfg, ins, CEE_BR);
4160 MONO_ADD_INS (bblock, ins);
4161 GET_BBLOCK (cfg, bbhash, tblock, target);
4162 link_bblock (cfg, bblock, tblock);
4163 CHECK_BBLOCK (target, ip, tblock);
4164 ins->inst_target_bb = tblock;
4165 start_new_bblock = 1;
4167 if (*ip == CEE_LEAVE)
4176 MONO_INST_NEW (cfg, ins, *ip);
4178 handle_loaded_temps (cfg, bblock, stack_start, sp);
4179 MONO_ADD_INS (bblock, ins);
4180 ins->cil_code = ip++;
4181 ins->inst_i0 = sp [0];
4182 ins->inst_i1 = sp [1];
4190 /* trampoline mono specific opcodes */
4191 case MONO_CUSTOM_PREFIX: {
4193 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
4197 case CEE_MONO_FUNC1: {
4199 gpointer func = NULL;
4204 case MONO_MARSHAL_CONV_STR_LPWSTR:
4205 func = mono_string_to_utf16;
4207 case MONO_MARSHAL_CONV_LPWSTR_STR:
4208 func = mono_string_from_utf16;
4210 case MONO_MARSHAL_CONV_LPSTR_STR:
4211 func = mono_string_new_wrapper;
4213 case MONO_MARSHAL_CONV_STR_LPTSTR:
4214 case MONO_MARSHAL_CONV_STR_LPSTR:
4215 func = mono_string_to_utf8;
4217 case MONO_MARSHAL_CONV_STR_BSTR:
4218 func = mono_string_to_bstr;
4220 case MONO_MARSHAL_CONV_STR_TBSTR:
4221 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
4222 func = mono_string_to_ansibstr;
4224 case MONO_MARSHAL_CONV_SB_LPSTR:
4225 func = mono_string_builder_to_utf8;
4227 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
4228 func = mono_array_to_savearray;
4230 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
4231 func = mono_array_to_lparray;
4233 case MONO_MARSHAL_CONV_DEL_FTN:
4234 func = mono_delegate_to_ftnptr;
4236 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
4237 func = mono_marshal_string_array;
4240 g_warning ("unknown conversion %d\n", ip [2]);
4241 g_assert_not_reached ();
4244 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4245 NEW_TEMPLOAD (cfg, *sp, temp);
4249 inline_costs += 10 * num_calls++;
4252 case CEE_MONO_PROC2: {
4253 gpointer func = NULL;
4258 case MONO_MARSHAL_CONV_LPSTR_SB:
4259 func = mono_string_utf8_to_builder;
4261 case MONO_MARSHAL_FREE_ARRAY:
4262 func = mono_marshal_free_array;
4265 g_assert_not_reached ();
4268 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4270 inline_costs += 10 * num_calls++;
4273 case CEE_MONO_PROC3: {
4274 gpointer func = NULL;
4279 case MONO_MARSHAL_CONV_STR_BYVALSTR:
4280 func = mono_string_to_byvalstr;
4282 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
4283 func = mono_string_to_byvalwstr;
4286 g_assert_not_reached ();
4289 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4291 inline_costs += 10 * num_calls++;
4297 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
4299 inline_costs += 10 * num_calls++;
4301 case CEE_MONO_LDPTR:
4302 CHECK_STACK_OVF (1);
4303 token = read32 (ip + 2);
4304 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
4308 inline_costs += 10 * num_calls++;
4310 case CEE_MONO_VTADDR:
4313 MONO_INST_NEW (cfg, ins, OP_VTADDR);
4315 ins->type = STACK_MP;
4316 ins->inst_left = *sp;
4320 case CEE_MONO_NEWOBJ: {
4321 MonoInst *iargs [2];
4323 CHECK_STACK_OVF (1);
4324 token = read32 (ip + 2);
4325 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4326 mono_class_init (klass);
4327 NEW_DOMAINCONST (cfg, iargs [0]);
4328 NEW_CLASSCONST (cfg, iargs [1], klass);
4329 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4330 NEW_TEMPLOAD (cfg, *sp, temp);
4333 inline_costs += 10 * num_calls++;
4336 case CEE_MONO_OBJADDR:
4339 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
4341 ins->type = STACK_MP;
4342 ins->inst_left = *sp;
4346 case CEE_MONO_LDNATIVEOBJ:
4348 token = read32 (ip + 2);
4349 klass = mono_method_get_wrapper_data (method, token);
4350 g_assert (klass->valuetype);
4351 mono_class_init (klass);
4352 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
4356 case CEE_MONO_RETOBJ:
4357 g_assert (cfg->ret);
4358 g_assert (method->signature->pinvoke);
4362 token = read32 (ip + 2);
4363 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4365 NEW_RETLOADA (cfg, ins);
4366 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
4368 if (sp != stack_start)
4371 MONO_INST_NEW (cfg, ins, CEE_BR);
4373 ins->inst_target_bb = end_bblock;
4374 MONO_ADD_INS (bblock, ins);
4375 link_bblock (cfg, bblock, end_bblock);
4376 start_new_bblock = 1;
4380 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
4388 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4397 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
4398 MONO_INST_NEW (cfg, ins, cmp->opcode);
4400 cmp->inst_i0 = sp [0];
4401 cmp->inst_i1 = sp [1];
4405 cmp->opcode = OP_COMPARE;
4407 ins->type = STACK_I4;
4417 CHECK_STACK_OVF (1);
4418 n = read32 (ip + 2);
4419 if (method->wrapper_type != MONO_WRAPPER_NONE)
4420 cmethod = mono_method_get_wrapper_data (method, n);
4422 cmethod = mono_get_method (image, n, NULL);
4425 * We can't do this in mono_ldftn, since it is used in
4426 * the synchronized wrapper, leading to an infinite loop.
4428 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
4429 cmethod = mono_marshal_get_synchronized_wrapper (cmethod);
4432 mono_class_init (cmethod->klass);
4433 handle_loaded_temps (cfg, bblock, stack_start, sp);
4435 NEW_METHODCONST (cfg, argconst, cmethod);
4436 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
4437 NEW_TEMPLOAD (cfg, *sp, temp);
4441 inline_costs += 10 * num_calls++;
4444 case CEE_LDVIRTFTN: {
4449 n = read32 (ip + 2);
4450 if (method->wrapper_type != MONO_WRAPPER_NONE)
4451 cmethod = mono_method_get_wrapper_data (method, n);
4453 cmethod = mono_get_method (image, n, NULL);
4455 mono_class_init (cmethod->klass);
4456 handle_loaded_temps (cfg, bblock, stack_start, sp);
4460 NEW_METHODCONST (cfg, args [1], cmethod);
4461 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
4462 NEW_TEMPLOAD (cfg, *sp, temp);
4466 inline_costs += 10 * num_calls++;
4470 CHECK_STACK_OVF (1);
4471 NEW_ARGLOAD (cfg, ins, read16 (ip + 2));
4477 CHECK_STACK_OVF (1);
4478 NEW_ARGLOADA (cfg, ins, read16 (ip + 2));
4486 handle_loaded_temps (cfg, bblock, stack_start, sp);
4487 n = read16 (ip + 2);
4488 NEW_ARGSTORE (cfg, ins, n, *sp);
4490 if (ins->opcode == CEE_STOBJ) {
4491 NEW_ARGLOADA (cfg, ins, n);
4492 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4494 MONO_ADD_INS (bblock, ins);
4498 CHECK_STACK_OVF (1);
4499 NEW_LOCLOAD (cfg, ins, read16 (ip + 2));
4505 CHECK_STACK_OVF (1);
4506 NEW_LOCLOADA (cfg, ins, read16 (ip + 2));
4514 n = read16 (ip + 2);
4515 handle_loaded_temps (cfg, bblock, stack_start, sp);
4516 NEW_LOCSTORE (cfg, ins, n, *sp);
4518 if (ins->opcode == CEE_STOBJ) {
4519 NEW_LOCLOADA (cfg, ins, n);
4520 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4522 MONO_ADD_INS (bblock, ins);
4529 if (sp != stack_start)
4531 MONO_INST_NEW (cfg, ins, 256 + ip [1]);
4532 ins->inst_left = *sp;
4535 if (header->init_locals)
4536 ins->flags |= MONO_INST_INIT;
4540 /* FIXME: set init flag if locals init is set in this method */
4542 case CEE_ENDFILTER: {
4543 MonoExceptionClause *clause, *nearest;
4544 int cc, nearest_num;
4548 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
4550 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
4551 ins->inst_left = *sp;
4553 MONO_ADD_INS (bblock, ins);
4554 start_new_bblock = 1;
4558 for (cc = 0; cc < header->num_clauses; ++cc) {
4559 clause = &header->clauses [cc];
4560 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
4561 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
4567 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
4571 case CEE_UNALIGNED_:
4572 ins_flag |= MONO_INST_UNALIGNED;
4576 ins_flag |= MONO_INST_VOLATILE;
4580 ins_flag |= MONO_INST_TAILCALL;
4586 token = read32 (ip + 2);
4587 if (method->wrapper_type != MONO_WRAPPER_NONE)
4588 klass = mono_method_get_wrapper_data (method, token);
4590 klass = mono_class_get (image, token);
4591 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
4597 MonoInst *iargs [3];
4603 handle_loaded_temps (cfg, bblock, stack_start, sp);
4604 if (ip [1] == CEE_CPBLK) {
4605 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4607 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
4615 /* FIXME: check we are in a catch handler */
4616 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
4617 load->cil_code = ip;
4618 MONO_INST_NEW (cfg, ins, CEE_THROW);
4619 ins->inst_left = load;
4621 MONO_ADD_INS (bblock, ins);
4623 start_new_bblock = 1;
4628 CHECK_STACK_OVF (1);
4629 token = read32 (ip + 2);
4630 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
4631 MonoType *type = mono_type_create_from_typespec (image, token);
4632 token = mono_type_size (type, &align);
4633 mono_metadata_free_type (type);
4635 MonoClass *szclass = mono_class_get (image, token);
4636 mono_class_init (szclass);
4637 token = mono_class_value_size (szclass, &align);
4639 NEW_ICONST (cfg, ins, token);
4644 case CEE_REFANYTYPE:
4645 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4648 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4653 g_error ("opcode 0x%02x not handled", *ip);
4656 if (start_new_bblock != 1)
4659 bblock->cil_length = ip - bblock->cil_code;
4660 bblock->next_bb = end_bblock;
4661 link_bblock (cfg, bblock, end_bblock);
4663 if (cfg->method == method && cfg->domainvar) {
4667 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
4668 call->signature = helper_sig_domain_get;
4669 call->inst.type = STACK_PTR;
4670 call->fptr = mono_domain_get;
4671 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
4673 MONO_ADD_INS (init_localsbb, store);
4676 if (header->init_locals) {
4678 for (i = 0; i < header->num_locals; ++i) {
4679 int t = header->locals [i]->type;
4680 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
4681 t = header->locals [i]->data.klass->enum_basetype->type;
4682 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
4683 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
4684 NEW_ICONST (cfg, ins, 0);
4685 NEW_LOCSTORE (cfg, store, i, ins);
4686 MONO_ADD_INS (init_localsbb, store);
4687 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
4688 MONO_INST_NEW (cfg, ins, OP_I8CONST);
4689 ins->type = STACK_I8;
4691 NEW_LOCSTORE (cfg, store, i, ins);
4692 MONO_ADD_INS (init_localsbb, store);
4693 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
4694 MONO_INST_NEW (cfg, ins, OP_R8CONST);
4695 ins->type = STACK_R8;
4696 ins->inst_p0 = (void*)&r8_0;
4697 NEW_LOCSTORE (cfg, store, i, ins);
4698 MONO_ADD_INS (init_localsbb, store);
4699 } else if (t == MONO_TYPE_VALUETYPE) {
4700 NEW_LOCLOADA (cfg, ins, i);
4701 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
4704 NEW_PCONST (cfg, ins, NULL);
4705 NEW_LOCSTORE (cfg, store, i, ins);
4706 MONO_ADD_INS (init_localsbb, store);
4712 /* resolve backward branches in the middle of an existing basic block */
4713 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
4715 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
4716 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
4717 if (tblock != start_bblock) {
4719 split_bblock (cfg, tblock, bblock);
4720 l = bblock->cil_code - header->code;
4721 bblock->cil_length = tblock->cil_length - l;
4722 tblock->cil_length = l;
4724 g_print ("recheck failed.\n");
4728 /* we compute regions here, because the length of filter clauses is not known in advance.
4729 * It is computed in the CEE_ENDFILTER case in the above switch statement*/
4730 if (cfg->method == method) {
4732 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4733 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
4734 if (cfg->verbose_level > 2)
4735 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
4738 g_hash_table_destroy (bbhash);
4741 dont_inline = g_list_remove (dont_inline, method);
4742 return inline_costs;
4745 if (cfg->method != method)
4746 g_hash_table_destroy (bbhash);
4747 dont_inline = g_list_remove (dont_inline, method);
4751 if (cfg->method != method)
4752 g_hash_table_destroy (bbhash);
4753 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
4754 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
4755 dont_inline = g_list_remove (dont_inline, method);
4760 mono_print_tree (MonoInst *tree) {
4766 arity = mono_burg_arity [tree->opcode];
4768 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
4770 switch (tree->opcode) {
4772 printf ("[%d]", tree->inst_c0);
4775 printf ("[%lld]", tree->inst_l);
4778 printf ("[%f]", *(double*)tree->inst_p0);
4781 printf ("[%f]", *(float*)tree->inst_p0);
4785 printf ("[%d]", tree->inst_c0);
4788 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
4791 printf ("[%s]", mono_arch_regname (tree->dreg));
4794 printf ("[%s]", tree->inst_newa_class->name);
4795 mono_print_tree (tree->inst_newa_len);
4806 case OP_VOIDCALLVIRT: {
4807 MonoCallInst *call = (MonoCallInst*)tree;
4809 printf ("[%s]", call->method->name);
4814 printf ("[%d (", tree->inst_c0);
4815 for (i = 0; i < tree->inst_phi_args [0]; i++) {
4818 printf ("%d", tree->inst_phi_args [i + 1]);
4830 printf ("[B%d]", tree->inst_target_bb->block_num);
4840 case OP_VOIDCALL_REG:
4841 mono_print_tree (tree->inst_left);
4853 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
4854 mono_print_tree (tree->inst_left);
4858 mono_print_tree (tree->inst_left);
4860 mono_print_tree (tree->inst_right);
4870 create_helper_signature (void)
4872 /* FIXME: set call conv */
4873 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
4874 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4875 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
4876 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
4877 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
4878 helper_sig_newarr->pinvoke = 1;
4880 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
4881 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4882 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
4883 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
4884 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
4885 helper_sig_newarr_specific->pinvoke = 1;
4887 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
4888 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4889 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
4890 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
4891 helper_sig_object_new->pinvoke = 1;
4893 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
4894 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4895 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
4896 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
4897 helper_sig_object_new_specific->pinvoke = 1;
4899 /* void* mono_method_compile (MonoMethod*) */
4900 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4901 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
4902 helper_sig_compile->pinvoke = 1;
4904 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
4905 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4906 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
4907 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
4908 helper_sig_compile_virt->pinvoke = 1;
4910 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
4911 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4912 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
4913 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
4914 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
4915 helper_sig_ldstr->pinvoke = 1;
4917 /* MonoDomain *mono_domain_get (void) */
4918 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
4919 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
4920 helper_sig_domain_get->pinvoke = 1;
4922 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
4923 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4924 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
4925 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
4926 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
4927 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
4928 helper_sig_stelem_ref->pinvoke = 1;
4930 /* long amethod (long, long) */
4931 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4932 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
4933 &mono_defaults.int64_class->byval_arg;
4934 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
4935 helper_sig_long_long_long->pinvoke = 1;
4937 /* object amethod (intptr) */
4938 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4939 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4940 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
4941 helper_sig_obj_ptr->pinvoke = 1;
4943 /* void amethod (intptr) */
4944 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4945 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4946 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
4947 helper_sig_void_ptr->pinvoke = 1;
4949 /* void amethod (MonoObject *obj) */
4950 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4951 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
4952 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
4953 helper_sig_void_obj->pinvoke = 1;
4955 /* intptr amethod (void) */
4956 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
4957 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
4958 helper_sig_ptr_void->pinvoke = 1;
4960 /* void amethod (intptr, intptr) */
4961 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4962 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4963 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
4964 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
4965 helper_sig_void_ptr_ptr->pinvoke = 1;
4967 /* void amethod (intptr, intptr, intptr) */
4968 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4969 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4970 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
4971 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
4972 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
4973 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
4975 /* intptr amethod (intptr, intptr) */
4976 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4977 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4978 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
4979 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
4980 helper_sig_ptr_ptr_ptr->pinvoke = 1;
4982 /* IntPtr amethod (object) */
4983 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4984 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
4985 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
4986 helper_sig_ptr_obj->pinvoke = 1;
4988 /* IntPtr amethod (int) */
4989 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4990 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
4991 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
4992 helper_sig_ptr_int->pinvoke = 1;
4994 /* long amethod (long, guint32) */
4995 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4996 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
4997 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
4998 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
4999 helper_sig_long_long_int->pinvoke = 1;
5001 /* ulong amethod (double) */
5002 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5003 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
5004 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
5005 helper_sig_ulong_double->pinvoke = 1;
5007 /* long amethod (double) */
5008 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5009 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
5010 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
5011 helper_sig_long_double->pinvoke = 1;
5013 /* uint amethod (double) */
5014 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5015 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
5016 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
5017 helper_sig_uint_double->pinvoke = 1;
5019 /* int amethod (double) */
5020 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5021 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
5022 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
5023 helper_sig_int_double->pinvoke = 1;
5025 /* void initobj (intptr, int size) */
5026 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5027 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
5028 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
5029 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
5030 helper_sig_initobj->pinvoke = 1;
5032 /* void memcpy (intptr, intptr, int size) */
5033 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5034 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
5035 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
5036 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
5037 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
5038 helper_sig_memcpy->pinvoke = 1;
5040 /* void memset (intptr, int val, int size) */
5041 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5042 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
5043 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
5044 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
5045 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
5046 helper_sig_memset->pinvoke = 1;
5049 static GHashTable *jit_icall_hash_name = NULL;
5050 static GHashTable *jit_icall_hash_addr = NULL;
5053 mono_find_jit_icall_by_name (const char *name)
5055 g_assert (jit_icall_hash_name);
5057 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
5058 return g_hash_table_lookup (jit_icall_hash_name, name);
5062 mono_find_jit_icall_by_addr (gconstpointer addr)
5064 g_assert (jit_icall_hash_addr);
5066 return g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
5070 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
5073 MonoMethod *wrapper;
5075 if (callinfo->wrapper)
5076 return callinfo->wrapper;
5077 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
5078 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
5079 callinfo->wrapper = mono_jit_compile_method (wrapper);
5081 return callinfo->wrapper;
5085 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
5087 MonoJitICallInfo *info;
5092 if (!jit_icall_hash_name) {
5093 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
5094 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
5097 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
5098 g_warning ("jit icall already defined \"%s\"\n", name);
5099 g_assert_not_reached ();
5102 info = g_new (MonoJitICallInfo, 1);
5109 #ifdef MONO_USE_EXC_TABLES
5110 || mono_arch_has_unwind_info (func)
5113 info->wrapper = func;
5115 info->wrapper = NULL;
5116 mono_icall_get_wrapper (info);
5119 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
5120 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
5121 if (func != info->wrapper)
5122 g_hash_table_insert (jit_icall_hash_addr, (gpointer)info->wrapper, info);
5127 static GHashTable *emul_opcode_hash = NULL;
5129 static MonoJitICallInfo *
5130 mono_find_jit_opcode_emulation (int opcode)
5132 if (emul_opcode_hash)
5133 return g_hash_table_lookup (emul_opcode_hash, (gpointer)opcode);
5139 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func)
5141 MonoJitICallInfo *info;
5143 if (!emul_opcode_hash)
5144 emul_opcode_hash = g_hash_table_new (NULL, NULL);
5146 g_assert (!sig->hasthis);
5147 g_assert (sig->param_count < 3);
5149 info = mono_register_jit_icall (func, name, sig, FALSE);
5151 g_hash_table_insert (emul_opcode_hash, (gpointer)opcode, info);
5155 decompose_foreach (MonoInst *tree, gpointer data)
5157 static MonoJitICallInfo *newarr_info = NULL;
5158 static MonoJitICallInfo *newarr_specific_info = NULL;
5159 MonoJitICallInfo *info;
5161 switch (tree->opcode) {
5163 MonoCompile *cfg = data;
5164 MonoInst *iargs [3];
5167 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
5168 g_assert (newarr_info);
5169 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
5170 g_assert (newarr_specific_info);
5173 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
5174 NEW_DOMAINCONST (cfg, iargs [0]);
5175 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
5176 iargs [2] = tree->inst_newa_len;
5181 MonoVTable *vtable = mono_class_vtable (cfg->domain,
5182 mono_array_class_get (&tree->inst_newa_class->byval_arg, 1));
5184 NEW_PCONST (cfg, iargs [0], vtable);
5185 iargs [1] = tree->inst_newa_len;
5187 info = newarr_specific_info;
5190 mono_emulate_opcode (cfg, tree, iargs, info);
5200 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
5202 switch (mono_burg_arity [tree->opcode]) {
5205 mono_inst_foreach (tree->inst_left, func, data);
5208 mono_inst_foreach (tree->inst_left, func, data);
5209 mono_inst_foreach (tree->inst_right, func, data);
5212 g_assert_not_reached ();
5219 mono_print_bb_code (MonoBasicBlock *bb) {
5221 MonoInst *c = bb->code;
5223 mono_print_tree (c);
5232 print_dfn (MonoCompile *cfg) {
5237 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
5239 for (i = 0; i < cfg->num_bblocks; ++i) {
5240 bb = cfg->bblocks [i];
5242 char* code1, *code2;
5243 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
5244 if (bb->last_ins->cil_code)
5245 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
5247 code2 = g_strdup ("");
5249 code1 [strlen (code1) - 1] = 0;
5250 code = g_strdup_printf ("%s -> %s", code1, code2);
5254 code = g_strdup ("\n");
5255 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
5257 MonoInst *c = bb->code;
5259 mono_print_tree (c);
5267 g_print ("\tprev:");
5268 for (j = 0; j < bb->in_count; ++j) {
5269 g_print (" BB%d", bb->in_bb [j]->block_num);
5271 g_print ("\t\tsucc:");
5272 for (j = 0; j < bb->out_count; ++j) {
5273 g_print (" BB%d", bb->out_bb [j]->block_num);
5275 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
5278 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
5281 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
5283 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
5291 * returns the offset used by spillvar. It allocates a new
5292 * spill variable if necessary.
5295 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
5297 MonoSpillInfo **si, *info;
5300 si = &cfg->spill_info;
5302 while (i <= spillvar) {
5305 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
5307 cfg->stack_offset -= sizeof (gpointer);
5308 info->offset = cfg->stack_offset;
5312 return (*si)->offset;
5318 g_assert_not_reached ();
5323 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
5327 g_assert (bb->code);
5328 bb->last_ins->next = inst;
5329 bb->last_ins = inst;
5331 bb->last_ins = bb->code = inst;
5336 mono_destroy_compile (MonoCompile *cfg)
5338 //mono_mempool_stats (cfg->mempool);
5339 g_hash_table_destroy (cfg->bb_hash);
5341 mono_regstate_free (cfg->rs);
5342 mono_mempool_destroy (cfg->mempool);
5343 g_list_free (cfg->ldstr_list);
5345 g_free (cfg->varinfo);
5351 mono_get_lmf_addr (void)
5353 MonoJitTlsData *jit_tls;
5355 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
5356 return &jit_tls->lmf;
5358 g_assert_not_reached ();
5363 * mono_thread_abort:
5364 * @obj: exception object
5366 * abort the thread, print exception information and stack trace
5369 mono_thread_abort (MonoObject *obj)
5371 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
5379 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
5381 MonoJitTlsData *jit_tls;
5384 jit_tls = g_new0 (MonoJitTlsData, 1);
5386 TlsSetValue (mono_jit_tls_id, jit_tls);
5388 jit_tls->abort_func = mono_thread_abort;
5389 jit_tls->end_of_stack = stack_start;
5391 lmf = g_new0 (MonoLMF, 1);
5397 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
5400 mono_thread_abort_dummy (MonoObject *obj)
5402 if (mono_thread_attach_aborted_cb)
5403 mono_thread_attach_aborted_cb (obj);
5405 mono_thread_abort (obj);
5409 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
5411 MonoJitTlsData *jit_tls;
5414 jit_tls = g_new0 (MonoJitTlsData, 1);
5416 TlsSetValue (mono_jit_tls_id, jit_tls);
5418 jit_tls->abort_func = mono_thread_abort_dummy;
5419 jit_tls->end_of_stack = stack_start;
5421 lmf = g_new0 (MonoLMF, 1);
5428 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
5430 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
5434 ji->data.target = target;
5435 ji->next = cfg->patch_info;
5437 cfg->patch_info = ji;
5441 mono_remove_patch_info (MonoCompile *cfg, int ip)
5443 MonoJumpInfo **ji = &cfg->patch_info;
5446 if ((*ji)->ip.i == ip)
5449 ji = &((*ji)->next);
5454 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
5455 MonoJitICallInfo *info;
5457 switch (mono_burg_arity [tree->opcode]) {
5460 dec_foreach (tree->inst_left, cfg);
5462 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5463 MonoInst *iargs [2];
5465 iargs [0] = tree->inst_left;
5467 mono_emulate_opcode (cfg, tree, iargs, info);
5473 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5474 MonoInst *iargs [2];
5476 iargs [0] = tree->inst_i0;
5477 iargs [1] = tree->inst_i1;
5479 mono_emulate_opcode (cfg, tree, iargs, info);
5481 dec_foreach (iargs [0], cfg);
5482 dec_foreach (iargs [1], cfg);
5485 dec_foreach (tree->inst_left, cfg);
5486 dec_foreach (tree->inst_right, cfg);
5490 g_assert_not_reached ();
5492 decompose_foreach (tree, cfg);
5496 decompose_pass (MonoCompile *cfg) {
5499 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5502 cfg->prev_ins = NULL;
5503 for (tree = cfg->cbb->code; tree; tree = tree->next) {
5504 dec_foreach (tree, cfg);
5505 cfg->prev_ins = tree;
5511 nullify_basic_block (MonoBasicBlock *bb)
5518 bb->code = bb->last_ins = NULL;
5522 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
5526 for (i = 0; i < bb->out_count; i++) {
5527 MonoBasicBlock *ob = bb->out_bb [i];
5528 for (j = 0; j < ob->in_count; j++) {
5529 if (ob->in_bb [j] == orig)
5530 ob->in_bb [j] = repl;
5537 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
5539 bb->out_count = bbn->out_count;
5540 bb->out_bb = bbn->out_bb;
5542 replace_basic_block (bb, bbn, bb);
5546 bb->last_ins->next = bbn->code;
5547 bb->last_ins = bbn->last_ins;
5550 bb->code = bbn->code;
5551 bb->last_ins = bbn->last_ins;
5553 bb->next_bb = bbn->next_bb;
5554 nullify_basic_block (bbn);
5558 optimize_branches (MonoCompile *cfg) {
5559 int changed = FALSE;
5560 MonoBasicBlock *bb, *bbn;
5565 /* we skip the entry block (exit is handled specially instead ) */
5566 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
5568 /* dont touch code inside exception clauses */
5569 if (bb->region != -1)
5572 if (bb->out_count == 1) {
5573 bbn = bb->out_bb [0];
5575 if (bb->region == bbn->region && bb->next_bb == bbn) {
5576 /* the block are in sequence anyway ... */
5579 * miguel: I do not understand what the test below does, could we
5580 * use a macro, or a comment here? opcode > CEE_BEQ && <= BLT_UN
5582 * It could also test for bb->last_in only once, and the value
5583 * could be cached (last_ins->opcode)
5585 if (bb->last_ins && (bb->last_ins->opcode == CEE_BR || (
5586 (bb->last_ins && bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN)))) {
5587 bb->last_ins->opcode = CEE_NOP;
5589 if (cfg->verbose_level > 2)
5590 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
5592 if (bbn->in_count == 1) {
5594 if (bbn != cfg->bb_exit) {
5595 if (cfg->verbose_level > 2)
5596 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
5597 merge_basic_blocks (bb, bbn);
5601 //mono_print_bb_code (bb);
5604 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
5605 bbn = bb->last_ins->inst_target_bb;
5606 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5607 bbn->code->inst_target_bb->region == bb->region) {
5609 if (cfg->verbose_level > 2)
5610 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
5611 bb->block_num, bbn->block_num);
5613 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
5614 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
5620 } else if (bb->out_count == 2) {
5621 /* fixme: this does not correctly - no idea whats wrong */
5622 if (0 && bb->last_ins && bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
5623 bbn = bb->last_ins->inst_true_bb;
5624 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5625 bbn->code->inst_target_bb->region == bb->region) {
5626 if (cfg->verbose_level > 2)
5627 g_print ("cbranch to branch triggered %d -> %d (0x%02x)\n", bb->block_num,
5628 bbn->block_num, bbn->code->opcode);
5630 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
5631 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
5635 bbn = bb->last_ins->inst_false_bb;
5636 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5637 bbn->code->inst_target_bb->region == bb->region) {
5638 if (cfg->verbose_level > 2)
5639 g_print ("cbranch to branch triggered %d -> %d (0x%02x)\n", bb->block_num,
5640 bbn->block_num, bbn->code->opcode);
5642 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
5643 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
5653 mono_compile_create_vars (MonoCompile *cfg)
5655 MonoMethodSignature *sig;
5656 MonoMethodHeader *header;
5659 header = ((MonoMethodNormal *)cfg->method)->header;
5661 sig = cfg->method->signature;
5663 if (!MONO_TYPE_IS_VOID (sig->ret)) {
5664 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
5665 cfg->ret->opcode = OP_RETARG;
5666 cfg->ret->inst_vtype = sig->ret;
5667 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
5671 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
5673 for (i = 0; i < sig->param_count; ++i)
5674 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
5676 cfg->locals_start = cfg->num_varinfo;
5678 for (i = 0; i < header->num_locals; ++i)
5679 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
5684 mono_print_code (MonoCompile *cfg)
5688 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5689 MonoInst *tree = bb->code;
5694 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
5696 for (; tree; tree = tree->next) {
5697 mono_print_tree (tree);
5702 bb->last_ins->next = NULL;
5707 extern const char * const mono_burg_rule_string [];
5710 emit_state (MonoCompile *cfg, MBState *state, int goal)
5713 int ern = mono_burg_rule (state, goal);
5714 const guint16 *nts = mono_burg_nts [ern];
5717 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
5721 // state->reg1 = state->reg2; /* chain rule */
5723 state->reg1 = mono_regstate_next_int (cfg->rs);
5724 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
5727 state->reg1 = mono_regstate_next_int (cfg->rs);
5728 state->reg2 = mono_regstate_next_int (cfg->rs);
5731 state->reg1 = mono_regstate_next_float (cfg->rs);
5738 mono_burg_kids (state, ern, kids);
5740 emit_state (cfg, kids [0], nts [0]);
5742 emit_state (cfg, kids [1], nts [1]);
5744 g_assert (!nts [3]);
5745 emit_state (cfg, kids [2], nts [2]);
5750 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
5751 if ((emit = mono_burg_func [ern]))
5752 emit (state, state->tree, cfg);
5755 #define DEBUG_SELECTION
5758 mini_select_instructions (MonoCompile *cfg)
5762 cfg->state_pool = mono_mempool_new ();
5763 cfg->rs = mono_regstate_new ();
5765 #ifdef DEBUG_SELECTION
5766 if (cfg->verbose_level >= 4) {
5767 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5768 MonoInst *tree = bb->code;
5769 g_print ("DUMP BLOCK %d:\n", bb->block_num);
5772 for (; tree; tree = tree->next) {
5773 mono_print_tree (tree);
5780 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5781 MonoInst *tree = bb->code, *next;
5787 bb->last_ins = NULL;
5790 mono_regstate_reset (cfg->rs);
5792 #ifdef DEBUG_SELECTION
5793 if (cfg->verbose_level >= 3)
5794 g_print ("LABEL BLOCK %d:\n", bb->block_num);
5796 for (; tree; tree = next) {
5798 #ifdef DEBUG_SELECTION
5799 if (cfg->verbose_level >= 3) {
5800 mono_print_tree (tree);
5805 if (!(mbstate = mono_burg_label (tree, cfg))) {
5806 g_warning ("unabled to label tree %p", tree);
5807 mono_print_tree (tree);
5809 g_assert_not_reached ();
5811 emit_state (cfg, mbstate, MB_NTERM_stmt);
5813 bb->max_ireg = cfg->rs->next_vireg;
5814 bb->max_freg = cfg->rs->next_vfreg;
5817 bb->last_ins->next = NULL;
5819 mono_mempool_empty (cfg->state_pool);
5821 mono_mempool_destroy (cfg->state_pool);
5825 mono_codegen (MonoCompile *cfg)
5827 MonoJumpInfo *patch_info;
5829 int i, max_epilog_size;
5832 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5833 cfg->spill_count = 0;
5834 /* we reuse dfn here */
5835 /* bb->dfn = bb_count++; */
5836 mono_arch_local_regalloc (cfg, bb);
5839 if (mono_trace_coverage)
5840 mono_allocate_coverage_info (cfg->method, cfg->num_bblocks);
5842 code = mono_arch_emit_prolog (cfg);
5844 if (mono_jit_profile)
5845 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
5847 cfg->code_len = code - cfg->native_code;
5848 cfg->prolog_end = cfg->code_len;
5850 mono_debug_open_method (cfg);
5852 /* emit code all basic blocks */
5853 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5854 bb->native_offset = cfg->code_len;
5855 mono_arch_output_basic_block (cfg, bb);
5857 cfg->bb_exit->native_offset = cfg->code_len;
5859 code = cfg->native_code + cfg->code_len;
5861 max_epilog_size = mono_arch_max_epilog_size (cfg);
5863 /* we always allocate code in cfg->domain->code_mp to increase locality */
5864 cfg->code_size = cfg->code_len + max_epilog_size;
5865 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
5866 code = mono_mempool_alloc (cfg->domain->code_mp, cfg->code_size);
5867 memcpy (code, cfg->native_code, cfg->code_len);
5868 g_free (cfg->native_code);
5869 cfg->native_code = code;
5870 code = cfg->native_code + cfg->code_len;
5872 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
5874 cfg->epilog_begin = cfg->code_len;
5876 if (mono_jit_profile)
5877 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
5879 cfg->code_len = code - cfg->native_code;
5881 mono_arch_emit_epilog (cfg);
5883 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5884 switch (patch_info->type) {
5885 case MONO_PATCH_INFO_ABS: {
5886 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
5888 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
5889 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
5890 patch_info->data.name = info->name;
5894 case MONO_PATCH_INFO_SWITCH: {
5895 gpointer *table = g_new (gpointer, patch_info->table_size);
5896 patch_info->ip.i = patch_info->ip.label->inst_c0;
5897 for (i = 0; i < patch_info->table_size; i++) {
5898 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
5900 patch_info->data.target = table;
5909 if (cfg->verbose_level > 1)
5910 g_print ("Method %s::%s emmitted at %p to %p\n", cfg->method->klass->name,
5911 cfg->method->name, cfg->native_code, cfg->native_code + cfg->code_len);
5913 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info);
5915 mono_debug_close_method (cfg);
5919 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
5924 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
5925 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
5927 if (cp->opcode == OP_ICONST) {
5928 if (cfg->opt & MONO_OPT_CONSPROP) {
5929 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
5933 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
5934 if (cfg->opt & MONO_OPT_COPYPROP) {
5935 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
5941 arity = mono_burg_arity [tree->opcode];
5944 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
5945 if (cfg->opt & MONO_OPT_CFOLD)
5946 mono_constant_fold_inst (tree, NULL);
5948 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
5949 if (cfg->opt & MONO_OPT_CFOLD)
5950 mono_constant_fold_inst (tree, NULL);
5952 mono_constant_fold_inst (tree, NULL);
5958 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
5962 switch (tree->opcode) {
5972 if (tree->ssa_op == MONO_SSA_NOP) {
5973 memset (acp, 0, sizeof (MonoInst *) * acp_size);
5990 case OP_VOIDCALL_REG:
5991 case OP_VOIDCALLVIRT:
5993 MonoCallInst *call = (MonoCallInst *)tree;
5994 MonoMethodSignature *sig = call->signature;
5995 int i, byref = FALSE;
5997 for (i = 0; i < sig->param_count; i++) {
5998 if (sig->params [i]->byref) {
6005 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6013 arity = mono_burg_arity [tree->opcode];
6019 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6022 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6023 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
6026 g_assert_not_reached ();
6031 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
6033 MonoInst *tree = bb->code;
6039 for (; tree; tree = tree->next) {
6041 mono_cprop_copy_values (cfg, tree, acp);
6043 mono_cprop_invalidate_values (tree, acp, acp_size);
6045 if (tree->ssa_op == MONO_SSA_STORE &&
6046 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
6047 MonoInst *i1 = tree->inst_i1;
6049 acp [tree->inst_i0->inst_c0] = NULL;
6051 for (i = 0; i < acp_size; i++) {
6052 if (acp [i] && acp [i]->opcode != OP_ICONST &&
6053 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
6058 if (i1->opcode == OP_ICONST) {
6059 acp [tree->inst_i0->inst_c0] = i1;
6060 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
6062 if (i1->ssa_op == MONO_SSA_LOAD &&
6063 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
6064 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
6065 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
6066 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
6071 if (tree->opcode == CEE_BEQ) {
6072 g_assert (tree->inst_i0->opcode == OP_COMPARE);
6073 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
6074 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
6076 tree->opcode = CEE_BR;
6077 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
6078 tree->inst_target_bb = tree->inst_true_bb;
6080 tree->inst_target_bb = tree->inst_false_bb;
6089 mono_local_cprop (MonoCompile *cfg)
6094 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
6096 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6097 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
6098 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
6103 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int parts)
6105 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
6106 guint8 *ip = (guint8 *)header->code;
6109 int dfn = 0, i, code_size_ratio;
6111 mono_jit_stats.methods_compiled++;
6112 if (mono_jit_profile)
6113 mono_profiler_method_jit (method);
6115 cfg = g_new0 (MonoCompile, 1);
6116 cfg->method = method;
6117 cfg->mempool = mono_mempool_new ();
6119 cfg->bb_hash = g_hash_table_new (g_direct_hash, NULL);
6120 cfg->domain = domain;
6121 cfg->verbose_level = mini_verbose;
6122 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
6123 ((MonoMethodNormal *)method)->header->max_stack);
6126 * create MonoInst* which represents arguments and local variables
6128 mono_compile_create_vars (cfg);
6130 if (cfg->verbose_level > 2)
6131 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
6133 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
6134 mono_destroy_compile (cfg);
6135 if (mono_jit_profile)
6136 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
6140 mono_jit_stats.basic_blocks += cfg->num_bblocks;
6141 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
6143 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
6145 /* Depth-first ordering on basic blocks */
6146 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
6148 if (cfg->opt & MONO_OPT_BRANCH)
6149 optimize_branches (cfg);
6151 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
6152 if (cfg->num_bblocks != dfn + 1) {
6153 if (cfg->verbose_level > 1)
6154 g_print ("unreachable code?\n");
6155 cfg->num_bblocks = dfn + 1;
6158 if (cfg->opt & MONO_OPT_LOOP) {
6159 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
6160 mono_compute_natural_loops (cfg);
6164 /* after method_to_ir */
6168 //#define DEBUGSSA "logic_run"
6169 #define DEBUGSSA_CLASS "Tests"
6173 if (!header->num_clauses && !cfg->disable_ssa) {
6174 mono_local_cprop (cfg);
6175 mono_ssa_compute (cfg);
6179 /* fixme: add all optimizations which requires SSA */
6180 if (cfg->opt & (MONO_OPT_DEADCE)) {
6181 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
6182 mono_local_cprop (cfg);
6183 mono_ssa_compute (cfg);
6185 if (cfg->verbose_level >= 2) {
6192 /* after SSA translation */
6196 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
6197 if (cfg->comp_done & MONO_COMP_SSA) {
6198 mono_ssa_cprop (cfg);
6200 mono_local_cprop (cfg);
6204 if (cfg->comp_done & MONO_COMP_SSA) {
6205 mono_ssa_deadce (cfg);
6207 //mono_ssa_strength_reduction (cfg);
6209 mono_ssa_remove (cfg);
6211 if (cfg->opt & MONO_OPT_BRANCH)
6212 optimize_branches (cfg);
6215 /* after SSA removal */
6219 decompose_pass (cfg);
6221 /* FIXME: disabled with exception clauses: bug #42136 */
6222 if ((!header->num_clauses) && (cfg->opt & MONO_OPT_LINEARS)) {
6225 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
6226 cfg->comp_done &= ~MONO_COMP_LIVENESS;
6227 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
6228 mono_analyze_liveness (cfg);
6230 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
6231 regs = mono_arch_get_global_int_regs (cfg);
6232 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
6236 //mono_print_code (cfg);
6240 /* variables are allocated after decompose, since decompose could create temps */
6241 mono_arch_allocate_vars (cfg);
6243 if (cfg->opt & MONO_OPT_CFOLD)
6244 mono_constant_fold (cfg);
6246 mini_select_instructions (cfg);
6249 if (cfg->verbose_level >= 2) {
6250 char *id = mono_method_full_name (cfg->method, FALSE);
6251 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
6255 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
6257 jinfo = g_new0 (MonoJitInfo, 1);
6258 jinfo->method = method;
6259 jinfo->code_start = cfg->native_code;
6260 jinfo->code_size = cfg->code_len;
6261 jinfo->used_regs = cfg->used_int_regs;
6263 if (header->num_clauses) {
6266 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
6267 jinfo->num_clauses = header->num_clauses;
6268 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
6269 sizeof (MonoJitExceptionInfo) * header->num_clauses);
6271 for (i = 0; i < header->num_clauses; i++) {
6272 MonoExceptionClause *ec = &header->clauses [i];
6273 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
6274 MonoBasicBlock *tblock;
6276 ei->flags = ec->flags;
6278 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
6279 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
6281 ei->data.filter = cfg->native_code + tblock->native_offset;
6283 ei->data.token = ec->token_or_filter;
6286 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
6288 ei->try_start = cfg->native_code + tblock->native_offset;
6289 g_assert (tblock->native_offset);
6290 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
6292 ei->try_end = cfg->native_code + tblock->native_offset;
6293 g_assert (tblock->native_offset);
6294 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
6296 ei->handler_start = cfg->native_code + tblock->native_offset;
6300 mono_jit_info_table_add (cfg->domain, jinfo);
6302 /* collect statistics */
6303 mono_jit_stats.allocated_code_size += cfg->code_len;
6304 code_size_ratio = cfg->code_len;
6305 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
6306 mono_jit_stats.biggest_method_size = code_size_ratio;
6307 mono_jit_stats.biggest_method = method;
6309 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
6310 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
6311 mono_jit_stats.max_code_size_ratio = code_size_ratio;
6312 mono_jit_stats.max_ratio_method = method;
6314 mono_jit_stats.native_code_size += cfg->code_len;
6316 if (mono_jit_profile)
6317 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
6323 mono_jit_compile_method (MonoMethod *method)
6325 /* FIXME: later copy the code from mono */
6326 MonoDomain *target_domain, *domain = mono_domain_get ();
6328 GHashTable *jit_code_hash;
6331 if (default_opt & MONO_OPT_SHARED)
6332 target_domain = mono_root_domain;
6334 target_domain = domain;
6336 jit_code_hash = target_domain->jit_code_hash;
6338 if ((code = g_hash_table_lookup (jit_code_hash, method))) {
6339 mono_jit_stats.methods_lookups++;
6343 #ifdef MONO_USE_AOT_COMPILER
6344 if (!mono_compile_aot) {
6345 mono_class_init (method->klass);
6346 if ((code = mono_aot_get_method (method))) {
6347 g_hash_table_insert (jit_code_hash, method, code);
6353 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
6354 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
6355 if (!method->info) {
6358 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
6359 mono_lookup_pinvoke_call (method);
6360 #ifdef MONO_USE_EXC_TABLES
6361 if (mono_method_blittable (method)) {
6362 method->info = method->addr;
6365 nm = mono_marshal_get_native_wrapper (method);
6366 method->info = mono_compile_method (nm);
6368 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
6369 //mono_debug_add_wrapper (method, nm);
6370 #ifdef MONO_USE_EXC_TABLES
6374 return method->info;
6375 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
6376 const char *name = method->name;
6379 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
6380 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
6381 /* FIXME: uhm, we need a wrapper to handle exceptions? */
6382 return (gpointer)mono_delegate_ctor;
6383 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
6384 nm = mono_marshal_get_delegate_invoke (method);
6385 return mono_jit_compile_method (nm);
6386 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
6387 nm = mono_marshal_get_delegate_begin_invoke (method);
6388 return mono_jit_compile_method (nm);
6389 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
6390 nm = mono_marshal_get_delegate_end_invoke (method);
6391 return mono_jit_compile_method (nm);
6397 cfg = mini_method_compile (method, default_opt, target_domain, 0);
6398 code = cfg->native_code;
6399 mono_destroy_compile (cfg);
6401 g_hash_table_insert (jit_code_hash, method, code);
6403 if (target_domain->jump_target_hash) {
6404 MonoJumpInfo patch_info;
6406 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
6408 patch_info.next = NULL;
6409 patch_info.ip.i = 0;
6410 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
6411 patch_info.data.method = method;
6412 g_hash_table_remove (target_domain->jump_target_hash, method);
6414 for (tmp = list; tmp; tmp = tmp->next)
6415 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info);
6416 g_slist_free (list);
6418 /* make sure runtime_init is called */
6419 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
6425 * mono_jit_runtime_invoke:
6426 * @method: the method to invoke
6427 * @obj: this pointer
6428 * @params: array of parameter values.
6429 * @exc: used to catch exceptions objects
6432 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
6435 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
6436 invoke = mono_marshal_get_runtime_invoke (method);
6437 runtime_invoke = mono_jit_compile_method (invoke);
6438 return runtime_invoke (obj, params, exc);
6441 #ifdef PLATFORM_WIN32
6442 #define GET_CONTEXT \
6443 struct sigcontext *ctx = (struct sigcontext*)_dummy;
6445 #define GET_CONTEXT \
6446 void **_p = (void **)&_dummy; \
6447 struct sigcontext *ctx = (struct sigcontext *)++_p;
6451 sigfpe_signal_handler (int _dummy)
6456 exc = mono_get_exception_divide_by_zero ();
6458 mono_arch_handle_exception (ctx, exc, FALSE);
6462 sigill_signal_handler (int _dummy)
6466 exc = mono_get_exception_execution_engine ("SIGILL");
6468 mono_arch_handle_exception (ctx, exc, FALSE);
6472 sigsegv_signal_handler (int _dummy)
6477 exc = mono_get_exception_null_reference ();
6479 mono_arch_handle_exception (ctx, exc, FALSE);
6483 sigusr1_signal_handler (int _dummy)
6488 thread = mono_thread_current ();
6490 g_assert (thread->abort_exc);
6492 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
6496 mono_runtime_install_handlers (void)
6498 #ifndef PLATFORM_WIN32
6499 struct sigaction sa;
6502 #ifdef PLATFORM_WIN32
6504 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
6505 win32_seh_set_handler(SIGILL, sigill_signal_handler);
6506 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
6507 #else /* !PLATFORM_WIN32 */
6509 /* libpthreads has its own implementation of sigaction(),
6510 * but it seems to work well with our current exception
6511 * handlers. If not we must call syscall directly instead
6515 sa.sa_handler = sigfpe_signal_handler;
6516 sigemptyset (&sa.sa_mask);
6518 //g_assert (syscall (SYS_sigaction, SIGFPE, &sa, NULL) != -1);
6519 g_assert (sigaction (SIGFPE, &sa, NULL) != -1);
6522 sa.sa_handler = sigill_signal_handler;
6523 sigemptyset (&sa.sa_mask);
6525 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
6526 g_assert (sigaction (SIGILL, &sa, NULL) != -1);
6528 /* catch thread abort signal */
6529 sa.sa_handler = sigusr1_signal_handler;
6530 sigemptyset (&sa.sa_mask);
6532 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
6533 g_assert (sigaction (mono_thread_get_abort_signal (), &sa, NULL) != -1);
6537 sa.sa_handler = sigsegv_signal_handler;
6538 sigemptyset (&sa.sa_mask);
6540 //g_assert (syscall (SYS_sigaction, SIGSEGV, &sa, NULL) != -1);
6541 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
6543 #endif /* PLATFORM_WIN32 */
6546 /* mono_jit_create_remoting_trampoline:
6547 * @method: pointer to the method info
6549 * Creates a trampoline which calls the remoting functions. This
6550 * is used in the vtable of transparent proxies.
6552 * Returns: a pointer to the newly created code
6555 mono_jit_create_remoting_trampoline (MonoMethod *method)
6558 guint8 *addr = NULL;
6560 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
6561 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
6562 nm = mono_marshal_get_remoting_invoke (method);
6563 addr = mono_compile_method (nm);
6565 addr = mono_compile_method (method);
6570 static CRITICAL_SECTION ms;
6573 mini_init (const char *filename)
6577 metadata_section = &ms;
6578 InitializeCriticalSection (metadata_section);
6580 mono_jit_tls_id = TlsAlloc ();
6581 mono_thread_start_cb (GetCurrentThreadId (), (gpointer)-1, NULL);
6585 mono_runtime_install_handlers ();
6587 mono_install_compile_method (mono_jit_compile_method);
6588 mono_install_trampoline (mono_arch_create_jit_trampoline);
6589 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
6590 mono_install_runtime_invoke (mono_jit_runtime_invoke);
6591 mono_install_handler (mono_arch_get_throw_exception ());
6592 mono_install_stack_walk (mono_jit_walk_stack);
6593 mono_install_get_config_dir ();
6595 domain = mono_init (filename);
6598 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
6599 ves_icall_get_frame_info);
6600 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
6601 ves_icall_get_trace);
6602 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
6603 mono_runtime_install_handlers);
6606 create_helper_signature ();
6608 mono_arch_register_lowlevel_calls ();
6609 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
6610 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
6612 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
6613 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
6615 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
6616 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
6617 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
6619 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
6620 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
6621 helper_sig_void_ptr, TRUE);
6624 * NOTE, NOTE, NOTE, NOTE:
6625 * when adding emulation for some opcodes, remember to also add a dummy
6626 * rule to the burg files, because we need the arity information to be correct.
6628 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult);
6629 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un);
6630 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf);
6631 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv);
6632 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un);
6633 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem);
6634 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un);
6636 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl);
6637 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr);
6638 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un);
6640 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8);
6641 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4);
6642 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8);
6643 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8);
6645 #if SIZEOF_VOID_P == 4
6646 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4);
6648 #warning "fixme: add opcode emulation"
6651 /* other jit icalls */
6652 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
6653 helper_sig_ptr_ptr_ptr, FALSE);
6654 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
6655 mono_register_jit_icall (mono_threads_get_static_data, "mono_threads_get_static_data", helper_sig_ptr_int, FALSE);
6656 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
6657 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
6658 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
6659 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
6660 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
6661 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
6662 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
6663 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
6664 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
6665 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
6666 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
6667 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
6668 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
6669 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
6670 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
6671 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
6672 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
6673 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
6674 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
6675 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
6676 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
6677 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
6678 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
6679 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
6680 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
6681 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
6682 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
6683 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
6685 mono_runtime_init (domain, mono_thread_start_cb,
6686 mono_thread_attach_cb);
6688 //mono_thread_attach (domain);
6692 MonoJitStats mono_jit_stats = {0};
6695 print_jit_stats (void)
6697 if (mono_jit_stats.enabled) {
6698 g_print ("Mono Jit statistics\n");
6699 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
6700 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
6701 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
6702 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
6703 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
6704 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
6705 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
6706 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
6707 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
6708 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
6709 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
6710 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
6711 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
6712 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
6713 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
6714 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
6715 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
6716 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
6718 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
6719 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
6720 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
6721 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
6722 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
6727 mini_cleanup (MonoDomain *domain)
6730 * mono_runtime_cleanup() and mono_domain_finalize () need to
6731 * be called early since they need the execution engine still
6732 * fully working (mono_domain_finalize may invoke managed finalizers
6733 * and mono_runtime_cleanup will wait for other threads to finish).
6735 mono_domain_finalize (domain);
6737 mono_runtime_cleanup (domain);
6739 mono_profiler_shutdown ();
6741 mono_debug_cleanup ();
6742 #ifdef PLATFORM_WIN32
6743 win32_seh_cleanup();
6746 mono_domain_unload (domain, TRUE);
6749 DeleteCriticalSection (metadata_section);
6753 mono_set_defaults (int verbose_level, guint32 opts)
6755 mini_verbose = verbose_level;