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) | 128 | clause->flags;
614 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
615 return (i << 8) | 64 | clause->flags;
619 /* search the try blocks */
620 for (i = 0; i < header->num_clauses; ++i) {
621 clause = &header->clauses [i];
622 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
623 return (i << 8) | clause->flags;
630 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
632 MonoMethod *method = cfg->method;
633 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
634 MonoExceptionClause *clause;
635 MonoBasicBlock *handler;
639 for (i = 0; i < header->num_clauses; ++i) {
640 clause = &header->clauses [i];
641 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
642 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
643 if (clause->flags == type) {
644 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
646 res = g_list_append (res, handler);
655 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
659 array [*dfn] = start;
660 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
661 for (i = 0; i < start->out_count; ++i) {
662 if (start->out_bb [i]->dfn)
665 start->out_bb [i]->dfn = *dfn;
666 start->out_bb [i]->df_parent = start;
667 array [*dfn] = start->out_bb [i];
668 df_visit (start->out_bb [i], dfn, array);
674 MonoBasicBlock *best;
678 previous_foreach (gconstpointer key, gpointer val, gpointer data)
680 PrevStruct *p = data;
681 MonoBasicBlock *bb = val;
682 //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,
683 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
685 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
689 static MonoBasicBlock*
690 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
696 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
701 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
710 * FIXME: take into account all the details:
711 * second may have been the target of more than one bblock
713 second->out_count = first->out_count;
714 second->out_bb = first->out_bb;
716 for (i = 0; i < first->out_count; ++i) {
717 bb = first->out_bb [i];
718 for (j = 0; j < bb->in_count; ++j) {
719 if (bb->in_bb [j] == first)
720 bb->in_bb [j] = second;
724 first->out_count = 0;
725 first->out_bb = NULL;
726 link_bblock (cfg, first, second);
728 second->last_ins = first->last_ins;
730 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
731 for (inst = first->code; inst && inst->next; inst = inst->next) {
732 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
733 g_print ("found %p: %s", inst->next->cil_code, code);
735 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
736 second->code = inst->next;
738 first->last_ins = inst;
739 second->next_bb = first->next_bb;
740 first->next_bb = second;
745 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
751 mono_type_to_ldind (MonoType *type)
763 case MONO_TYPE_BOOLEAN:
777 case MONO_TYPE_FNPTR:
779 case MONO_TYPE_CLASS:
780 case MONO_TYPE_STRING:
781 case MONO_TYPE_OBJECT:
782 case MONO_TYPE_SZARRAY:
783 case MONO_TYPE_ARRAY:
784 return CEE_LDIND_REF;
792 case MONO_TYPE_VALUETYPE:
793 if (type->data.klass->enumtype) {
794 t = type->data.klass->enum_basetype->type;
799 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
805 mono_type_to_stind (MonoType *type)
816 case MONO_TYPE_BOOLEAN:
828 case MONO_TYPE_FNPTR:
830 case MONO_TYPE_CLASS:
831 case MONO_TYPE_STRING:
832 case MONO_TYPE_OBJECT:
833 case MONO_TYPE_SZARRAY:
834 case MONO_TYPE_ARRAY:
835 return CEE_STIND_REF;
843 case MONO_TYPE_VALUETYPE:
844 if (type->data.klass->enumtype) {
845 t = type->data.klass->enum_basetype->type;
851 g_error ("unknown type %02x in type_to_stind", type->type);
857 * Returns the type used in the eval stack when @type is loaded.
858 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
861 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
865 inst->type = STACK_MP;
873 case MONO_TYPE_BOOLEAN:
879 inst->type = STACK_I4;
884 case MONO_TYPE_FNPTR:
885 inst->type = STACK_PTR;
887 case MONO_TYPE_CLASS:
888 case MONO_TYPE_STRING:
889 case MONO_TYPE_OBJECT:
890 case MONO_TYPE_SZARRAY:
891 case MONO_TYPE_ARRAY:
892 inst->type = STACK_OBJ;
896 inst->type = STACK_I8;
900 inst->type = STACK_R8;
902 case MONO_TYPE_VALUETYPE:
903 if (type->data.klass->enumtype) {
904 t = type->data.klass->enum_basetype->type;
907 inst->klass = type->data.klass;
908 inst->type = STACK_VTYPE;
912 g_error ("unknown type 0x%02x in eval stack type", type->type);
917 * The following tables are used to quickly validate the IL code in type_from_op ().
920 bin_num_table [STACK_MAX] [STACK_MAX] = {
921 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
922 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
923 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
924 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
925 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
926 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
927 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
928 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
933 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
936 /* reduce the size of this table */
938 bin_int_table [STACK_MAX] [STACK_MAX] = {
939 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
940 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
941 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
942 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
943 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
944 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
945 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, 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}
950 bin_comp_table [STACK_MAX] [STACK_MAX] = {
952 {0, 1, 0, 1, 0, 0, 4, 0},
953 {0, 0, 1, 0, 0, 0, 0, 0},
954 {0, 1, 0, 1, 0, 2, 4, 0},
955 {0, 0, 0, 0, 1, 0, 0, 0},
956 {0, 0, 0, 2, 0, 1, 0, 0},
957 {0, 4, 0, 4, 0, 0, 3, 0},
958 {0, 0, 0, 0, 0, 0, 0, 0},
961 /* reduce the size of this table */
963 shift_table [STACK_MAX] [STACK_MAX] = {
964 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
965 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
966 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
967 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
968 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
969 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
970 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, 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}
975 * Tables to map from the non-specific opcode to the matching
976 * type-specific opcode.
978 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
980 binops_op_map [STACK_MAX] = {
981 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
984 /* handles from CEE_NEG to CEE_CONV_U8 */
986 unops_op_map [STACK_MAX] = {
987 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
990 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
992 ovfops_op_map [STACK_MAX] = {
993 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
996 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
998 ovf2ops_op_map [STACK_MAX] = {
999 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
1002 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1003 static const guint16
1004 ovf3ops_op_map [STACK_MAX] = {
1005 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
1008 /* handles from CEE_CEQ to CEE_CLT_UN */
1009 static const guint16
1010 ceqops_op_map [STACK_MAX] = {
1011 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1015 * Sets ins->type (the type on the eval stack) according to the
1016 * type of the opcode and the arguments to it.
1017 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1019 * FIXME: this function sets ins->type unconditionally in some cases, but
1020 * it should set it to invalid for some types (a conv.x on an object)
1023 type_from_op (MonoInst *ins) {
1024 switch (ins->opcode) {
1031 /* FIXME: check unverifiable args for STACK_MP */
1032 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1033 ins->opcode += binops_op_map [ins->type];
1040 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1041 ins->opcode += binops_op_map [ins->type];
1046 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1047 ins->opcode += binops_op_map [ins->type];
1050 /* FIXME: handle some specifics with ins->next->type */
1051 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1055 case 256+CEE_CGT_UN:
1057 case 256+CEE_CLT_UN:
1058 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1059 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1063 ins->type = neg_table [ins->inst_i0->type];
1064 ins->opcode += unops_op_map [ins->type];
1067 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1068 ins->type = ins->inst_i0->type;
1070 ins->type = STACK_INV;
1071 ins->opcode += unops_op_map [ins->type];
1077 ins->type = STACK_I4;
1078 ins->opcode += unops_op_map [ins->inst_i0->type];
1081 ins->type = STACK_R8;
1082 switch (ins->inst_i0->type) {
1087 ins->opcode = OP_LCONV_TO_R_UN;
1091 case CEE_CONV_OVF_I1:
1092 case CEE_CONV_OVF_U1:
1093 case CEE_CONV_OVF_I2:
1094 case CEE_CONV_OVF_U2:
1095 case CEE_CONV_OVF_I4:
1096 case CEE_CONV_OVF_U4:
1097 ins->type = STACK_I4;
1098 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1100 case CEE_CONV_OVF_I_UN:
1101 case CEE_CONV_OVF_U_UN:
1102 ins->type = STACK_PTR;
1103 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1105 case CEE_CONV_OVF_I1_UN:
1106 case CEE_CONV_OVF_I2_UN:
1107 case CEE_CONV_OVF_I4_UN:
1108 case CEE_CONV_OVF_U1_UN:
1109 case CEE_CONV_OVF_U2_UN:
1110 case CEE_CONV_OVF_U4_UN:
1111 ins->type = STACK_I4;
1112 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1115 ins->type = STACK_PTR;
1116 switch (ins->inst_i0->type) {
1122 ins->opcode = OP_LCONV_TO_U;
1125 ins->opcode = OP_FCONV_TO_U;
1131 ins->type = STACK_I8;
1132 ins->opcode += unops_op_map [ins->inst_i0->type];
1134 case CEE_CONV_OVF_I8:
1135 case CEE_CONV_OVF_U8:
1136 ins->type = STACK_I8;
1137 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1139 case CEE_CONV_OVF_U8_UN:
1140 case CEE_CONV_OVF_I8_UN:
1141 ins->type = STACK_I8;
1142 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1146 ins->type = STACK_R8;
1147 ins->opcode += unops_op_map [ins->inst_i0->type];
1150 ins->type = STACK_R8;
1154 ins->type = STACK_I4;
1155 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1158 case CEE_CONV_OVF_I:
1159 case CEE_CONV_OVF_U:
1160 ins->type = STACK_PTR;
1161 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1164 case CEE_ADD_OVF_UN:
1166 case CEE_MUL_OVF_UN:
1168 case CEE_SUB_OVF_UN:
1169 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1170 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1173 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1180 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1183 /* map ldelem.x to the matching ldind.x opcode */
1185 ldelem_to_ldind [] = {
1199 /* map stelem.x to the matching stind.x opcode */
1201 stelem_to_stind [] = {
1215 param_table [STACK_MAX] [STACK_MAX] = {
1220 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1224 switch (args->type) {
1234 for (i = 0; i < sig->param_count; ++i) {
1235 switch (args [i].type) {
1239 if (!sig->params [i]->byref)
1243 if (sig->params [i]->byref)
1245 switch (sig->params [i]->type) {
1246 case MONO_TYPE_CLASS:
1247 case MONO_TYPE_STRING:
1248 case MONO_TYPE_OBJECT:
1249 case MONO_TYPE_SZARRAY:
1250 case MONO_TYPE_ARRAY:
1257 if (sig->params [i]->byref)
1259 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1268 /*if (!param_table [args [i].type] [sig->params [i]->type])
1276 * When we need a pointer to the current domain many times in a method, we
1277 * call mono_domain_get() once and we store the result in a local variable.
1278 * This function returns the variable that represents the MonoDomain*.
1280 inline static MonoInst *
1281 mono_get_domainvar (MonoCompile *cfg)
1283 if (!cfg->domainvar)
1284 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1285 return cfg->domainvar;
1289 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1292 int num = cfg->num_varinfo;
1294 if ((num + 1) >= cfg->varinfo_count) {
1295 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1296 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1297 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1300 mono_jit_stats.allocate_var++;
1302 MONO_INST_NEW (cfg, inst, opcode);
1303 inst->inst_c0 = num;
1304 inst->inst_vtype = type;
1305 inst->klass = mono_class_from_mono_type (type);
1306 /* if set to 1 the variable is native */
1309 cfg->varinfo [num] = inst;
1311 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1312 MONO_INIT_VARINFO (cfg->vars [num], num);
1315 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1320 type_from_stack_type (MonoInst *ins) {
1321 switch (ins->type) {
1322 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1323 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1324 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1325 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1326 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1327 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1328 case STACK_VTYPE: return &ins->klass->byval_arg;
1330 g_error ("stack type %d to montype not handled\n", ins->type);
1336 array_access_to_klass (int opcode)
1340 return mono_defaults.byte_class;
1342 return mono_defaults.uint16_class;
1345 return mono_defaults.int_class;
1348 return mono_defaults.sbyte_class;
1351 return mono_defaults.int16_class;
1354 return mono_defaults.int32_class;
1356 return mono_defaults.uint32_class;
1359 return mono_defaults.int64_class;
1362 return mono_defaults.single_class;
1365 return mono_defaults.double_class;
1366 case CEE_LDELEM_REF:
1367 case CEE_STELEM_REF:
1368 return mono_defaults.object_class;
1370 g_assert_not_reached ();
1376 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1380 MONO_ADD_INS (bb, inst);
1383 switch (bb->last_ins->opcode) {
1396 while (prev->next && prev->next != bb->last_ins)
1398 if (prev == bb->code) {
1399 if (bb->last_ins == bb->code) {
1400 inst->next = bb->code;
1403 inst->next = prev->next;
1407 inst->next = bb->last_ins;
1411 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1413 MONO_ADD_INS (bb, inst);
1419 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1421 MonoInst *inst, *load;
1423 NEW_TEMPLOAD (cfg, load, src);
1425 NEW_TEMPSTORE (cfg, inst, dest, load);
1426 if (inst->opcode == CEE_STOBJ) {
1427 NEW_TEMPLOADA (cfg, inst, dest);
1428 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1430 inst->cil_code = NULL;
1431 mono_add_ins_to_end (bb, inst);
1436 * This function is called to handle items that are left on the evaluation stack
1437 * at basic block boundaries. What happens is that we save the values to local variables
1438 * and we reload them later when first entering the target basic block (with the
1439 * handle_loaded_temps () function).
1440 * A single joint point will use the same variables (stored in the array bb->out_stack or
1441 * bb->in_stack, if the basic block is before or after the joint point).
1444 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1446 MonoBasicBlock *outb;
1447 MonoInst *inst, **locals;
1451 if (cfg->verbose_level > 3)
1452 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1453 if (!bb->out_scount) {
1455 bb->out_scount = count;
1456 //g_print ("bblock %d has out:", bb->block_num);
1457 for (i = 0; i < bb->out_count; ++i) {
1458 outb = bb->out_bb [i];
1459 //g_print (" %d", outb->block_num);
1460 if (outb->in_stack) {
1462 bb->out_stack = outb->in_stack;
1468 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1469 for (i = 0; i < count; ++i) {
1471 * dietmar suggests that we can reuse temps already allocated
1472 * for this purpouse, if they occupy the same stack slot and if
1473 * they are of the same type.
1475 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1479 locals = bb->out_stack;
1480 for (i = 0; i < count; ++i) {
1481 /* add store ops at the end of the bb, before the branch */
1482 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1483 if (inst->opcode == CEE_STOBJ) {
1484 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1485 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1487 inst->cil_code = sp [i]->cil_code;
1488 mono_add_ins_to_end (bb, inst);
1490 if (cfg->verbose_level > 3)
1491 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1494 for (i = 0; i < bb->out_count; ++i) {
1495 outb = bb->out_bb [i];
1496 if (outb->in_scount)
1497 continue; /* check they are the same locals */
1498 outb->in_scount = count;
1499 outb->in_stack = locals;
1505 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1510 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1514 case MONO_TYPE_VOID:
1515 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1518 case MONO_TYPE_BOOLEAN:
1521 case MONO_TYPE_CHAR:
1524 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1528 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1529 case MONO_TYPE_CLASS:
1530 case MONO_TYPE_STRING:
1531 case MONO_TYPE_OBJECT:
1532 case MONO_TYPE_SZARRAY:
1533 case MONO_TYPE_ARRAY:
1534 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1537 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1540 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1541 case MONO_TYPE_VALUETYPE:
1542 if (type->data.klass->enumtype) {
1543 t = type->data.klass->enum_basetype->type;
1546 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1548 g_error ("unknown type %02x in ret_type_to_call_opcode", type->type);
1554 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1556 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1558 ji->ip.label = label;
1559 ji->type = MONO_PATCH_INFO_SWITCH;
1560 ji->data.table = bbs;
1561 ji->next = cfg->patch_info;
1562 ji->table_size = num_blocks;
1563 cfg->patch_info = ji;
1567 * When we add a tree of instructions, we need to ensure the instructions currently
1568 * on the stack are executed before (like, if we load a value from a local).
1569 * We ensure this by saving the currently loaded values to temps and rewriting the
1570 * instructions to load the values.
1571 * This is not done for opcodes that terminate a basic block (because it's handled already
1572 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1575 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1577 MonoInst *load, *store, *temp, *ins;
1579 while (stack < sp) {
1581 /* handle also other constants */
1582 if (ins->opcode != OP_ICONST) {
1583 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1584 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1585 store->cil_code = ins->cil_code;
1586 if (store->opcode == CEE_STOBJ) {
1587 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1588 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1590 MONO_ADD_INS (bblock, store);
1591 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1592 load->cil_code = ins->cil_code;
1600 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1601 const guint8 *ip, gboolean to_end)
1603 MonoInst *temp, *store, *ins = (MonoInst*)call;
1604 MonoType *ret = sig->ret;
1606 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1608 call->inst.type = STACK_OBJ;
1609 call->inst.opcode = CEE_CALL;
1610 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1612 type_to_eval_stack_type (ret, ins);
1613 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1616 if (MONO_TYPE_ISSTRUCT (ret)) {
1619 /* we use this to allocate native sized structs */
1620 temp->unused = sig->pinvoke;
1622 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1623 if (call->inst.opcode == OP_VCALL)
1624 ins->inst_left = loada;
1626 ins->inst_right = loada; /* a virtual or indirect call */
1629 mono_add_ins_to_end (bblock, ins);
1631 MONO_ADD_INS (bblock, ins);
1633 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1634 store->cil_code = ip;
1636 mono_add_ins_to_end (bblock, store);
1638 MONO_ADD_INS (bblock, store);
1640 return temp->inst_c0;
1643 mono_add_ins_to_end (bblock, ins);
1645 MONO_ADD_INS (bblock, ins);
1650 inline static MonoCallInst *
1651 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1652 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1657 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1659 call->inst.cil_code = ip;
1661 call->signature = sig;
1662 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1664 for (i = 0; i < (sig->param_count + sig->hasthis); ++i) {
1665 if (call->args [i]) {
1666 if (!call->args [i]->cil_code)
1667 call->args [i]->cil_code = ip;
1669 mono_add_ins_to_end (bblock, call->args [i]);
1671 MONO_ADD_INS (bblock, call->args [i]);
1678 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1679 MonoInst **args, MonoInst *addr, const guint8 *ip)
1681 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1683 call->inst.inst_i0 = addr;
1685 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1688 static MonoCallInst*
1689 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1690 MonoInst **args, const guint8 *ip, MonoInst *this)
1692 gboolean virtual = this != NULL;
1695 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
1697 if (this && sig->hasthis &&
1698 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
1699 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
1700 call->method = mono_marshal_get_remoting_invoke_with_check (method);
1702 call->method = method;
1704 call->inst.flags |= MONO_INST_HAS_METHOD;
1705 call->inst.inst_left = this;
1711 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
1712 MonoInst **args, const guint8 *ip, MonoInst *this)
1714 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, method->signature, args, ip, this);
1716 return mono_spill_call (cfg, bblock, call, method->signature, method->string_ctor, ip, FALSE);
1720 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
1721 MonoInst **args, const guint8 *ip, gboolean to_end)
1727 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
1729 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
1733 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
1735 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
1738 g_warning ("unregistered JIT ICall");
1739 g_assert_not_reached ();
1742 return mono_emit_native_call (cfg, bblock, info->wrapper, info->sig, args, ip, FALSE);
1746 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
1748 MonoInst *ins, *temp = NULL, *store, *load;
1752 /*g_print ("emulating: ");
1753 mono_print_tree (tree);
1755 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
1756 ins = (MonoInst*)call;
1758 call->inst.cil_code = tree->cil_code;
1760 call->signature = info->sig;
1762 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
1764 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1765 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
1766 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1767 store->cil_code = tree->cil_code;
1772 nargs = info->sig->param_count + info->sig->hasthis;
1774 for (i = 1; i < nargs; i++) {
1775 call->args [i - 1]->next = call->args [i];
1779 call->args [nargs - 1]->next = store;
1781 if (cfg->prev_ins) {
1782 store->next = cfg->prev_ins->next;
1784 cfg->prev_ins->next = call->args [0];
1786 cfg->prev_ins->next = store;
1788 store->next = cfg->cbb->code;
1790 cfg->cbb->code = call->args [0];
1792 cfg->cbb->code = store;
1796 call->fptr = info->wrapper;
1798 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1799 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1804 static MonoMethodSignature *
1805 mono_get_element_address_signature (int arity)
1807 static GHashTable *sighash = NULL;
1808 MonoMethodSignature *res;
1812 sighash = g_hash_table_new (NULL, NULL);
1815 if ((res = g_hash_table_lookup (sighash, (gpointer)arity)))
1818 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
1820 res->params [0] = &mono_defaults.array_class->byval_arg;
1822 for (i = 1; i <= arity; i++)
1823 res->params [i] = &mono_defaults.int_class->byval_arg;
1825 res->ret = &mono_defaults.int_class->byval_arg;
1827 g_hash_table_insert (sighash, (gpointer)arity, res);
1833 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
1834 MonoInst *iargs [3];
1839 * This check breaks with spilled vars... need to handle it during verification anyway.
1840 * g_assert (klass && klass == src->klass && klass == dest->klass);
1844 n = mono_class_native_size (klass, NULL);
1846 n = mono_class_value_size (klass, NULL);
1850 NEW_ICONST (cfg, iargs [2], n);
1852 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
1856 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
1858 MonoInst *iargs [2];
1859 MonoInst *ins, *zero_int32;
1862 NEW_ICONST (cfg, zero_int32, 0);
1864 mono_class_init (klass);
1865 n = mono_class_value_size (klass, NULL);
1866 MONO_INST_NEW (cfg, ins, 0);
1868 ins->inst_left = dest;
1869 ins->inst_right = zero_int32;
1872 ins->opcode = CEE_STIND_I1;
1873 MONO_ADD_INS (bblock, ins);
1876 ins->opcode = CEE_STIND_I2;
1877 MONO_ADD_INS (bblock, ins);
1880 ins->opcode = CEE_STIND_I4;
1881 MONO_ADD_INS (bblock, ins);
1884 handle_loaded_temps (cfg, bblock, stack_start, sp);
1885 NEW_ICONST (cfg, ins, n);
1888 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
1893 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
1896 mono_method_check_inlining (MonoMethod *method)
1898 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
1899 MonoMethodSignature *signature = method->signature;
1902 /* fixme: we should inline wrappers */
1903 if (method->wrapper_type != MONO_WRAPPER_NONE)
1906 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1907 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1908 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
1909 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
1910 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1911 (method->klass->marshalbyref) ||
1912 !header || header->num_clauses ||
1913 /* fixme: why cant we inline valuetype returns? */
1914 MONO_TYPE_ISSTRUCT (signature->ret))
1917 /* its not worth to inline methods with valuetype arguments?? */
1918 for (i = 0; i < signature->param_count; i++) {
1919 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
1924 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
1926 /* also consider num_locals? */
1927 if (header->code_size < 20)
1934 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
1939 if (cmethod->klass == mono_defaults.string_class) {
1940 if (cmethod->name [0] != 'g' || strcmp (cmethod->name, "get_Chars"))
1943 } else if (cmethod->klass == mono_defaults.math_class) {
1944 if (strcmp (cmethod->name, "Sin") == 0)
1946 else if (strcmp (cmethod->name, "Cos") == 0)
1948 else if (strcmp (cmethod->name, "Tan") == 0)
1950 else if (strcmp (cmethod->name, "Atan") == 0)
1952 else if (strcmp (cmethod->name, "Sqrt") == 0)
1954 else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8)
1961 pc = fsig->param_count + fsig->hasthis;
1962 MONO_INST_NEW (cfg, ins, op);
1965 ins->inst_i0 = args [0];
1967 ins->inst_i1 = args [1];
1974 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
1976 MonoInst *store, *temp;
1979 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
1981 if (!sig->hasthis && sig->param_count == 0)
1985 if (sp [0]->opcode == OP_ICONST) {
1988 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
1990 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
1991 store->cil_code = sp [0]->cil_code;
1992 MONO_ADD_INS (bblock, store);
1997 for (i = 0; i < sig->param_count; ++i) {
1998 if (sp [0]->opcode == OP_ICONST) {
2001 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2003 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2004 store->cil_code = sp [0]->cil_code;
2005 if (store->opcode == CEE_STOBJ) {
2006 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2007 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2009 MONO_ADD_INS (bblock, store);
2017 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2018 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b)
2020 MonoInst *ins, *rvar = NULL;
2021 MonoMethodHeader *cheader;
2022 MonoBasicBlock *ebblock, *sbblock;
2023 int i, costs, new_locals_offset;
2025 if (cfg->verbose_level > 2)
2026 g_print ("INLINE START %p %s\n", cmethod, mono_method_full_name (cmethod, TRUE));
2028 cheader = ((MonoMethodNormal *)cmethod)->header;
2030 if (!cmethod->inline_info) {
2031 mono_jit_stats.inlineable_methods++;
2032 cmethod->inline_info = 1;
2034 /* allocate space to store the return value */
2035 if (!MONO_TYPE_IS_VOID (fsig->ret))
2036 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2038 /* allocate local variables */
2039 new_locals_offset = cfg->num_varinfo;
2040 for (i = 0; i < cheader->num_locals; ++i)
2041 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2043 /* allocate starte and end blocks */
2044 sbblock = NEW_BBLOCK (cfg);
2045 sbblock->block_num = cfg->num_bblocks++;
2046 sbblock->real_offset = real_offset;
2048 ebblock = NEW_BBLOCK (cfg);
2049 ebblock->block_num = cfg->num_bblocks++;
2050 ebblock->real_offset = real_offset;
2052 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2054 if (costs >= 0 && costs < 60) {
2055 if (cfg->verbose_level > 2)
2056 g_print ("INLINE END %s\n", mono_method_full_name (cmethod, TRUE));
2058 mono_jit_stats.inlined_methods++;
2060 /* always add some code to avoid block split failures */
2061 MONO_INST_NEW (cfg, ins, CEE_NOP);
2062 MONO_ADD_INS (bblock, ins);
2065 bblock->next_bb = sbblock;
2066 link_bblock (cfg, bblock, sbblock);
2069 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2075 if (cfg->verbose_level > 2)
2076 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2082 * Some of these comments may well be out-of-date.
2083 * Design decisions: we do a single pass over the IL code (and we do bblock
2084 * splitting/merging in the few cases when it's required: a back jump to an IL
2085 * address that was not already seen as bblock starting point).
2086 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2087 * Complex operations are decomposed in simpler ones right away. We need to let the
2088 * arch-specific code peek and poke inside this process somehow (except when the
2089 * optimizations can take advantage of the full semantic info of coarse opcodes).
2090 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2091 * MonoInst->opcode initially is the IL opcode or some simplification of that
2092 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2093 * opcode with value bigger than OP_LAST.
2094 * At this point the IR can be handed over to an interpreter, a dumb code generator
2095 * or to the optimizing code generator that will translate it to SSA form.
2097 * Profiling directed optimizations.
2098 * We may compile by default with few or no optimizations and instrument the code
2099 * or the user may indicate what methods to optimize the most either in a config file
2100 * or through repeated runs where the compiler applies offline the optimizations to
2101 * each method and then decides if it was worth it.
2104 * * consider using an array instead of an hash table (bb_hash)
2107 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2108 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2109 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2111 /* offset from br.s -> br like opcodes */
2112 #define BIG_BRANCH_OFFSET 13
2115 * mono_method_to_ir: translates IL into basic blocks containing trees
2118 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2119 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2120 guint inline_offset, gboolean is_virtual_call)
2122 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2123 MonoInst *ins, **sp, **stack_start;
2124 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2126 MonoMethod *cmethod;
2127 MonoInst **arg_array;
2128 MonoMethodHeader *header;
2130 guint32 token, ins_flag;
2132 unsigned char *ip, *end, *target;
2133 static double r8_0 = 0.0;
2134 MonoMethodSignature *sig;
2135 MonoType **param_types;
2136 GList *bb_recheck = NULL, *tmp;
2137 int i, n, start_new_bblock, align;
2138 int num_calls = 0, inline_costs = 0;
2139 int *filter_lengths = NULL;
2140 int breakpoint_id = 0;
2143 image = method->klass->image;
2144 header = ((MonoMethodNormal *)method)->header;
2145 sig = method->signature;
2146 ip = (unsigned char*)header->code;
2147 end = ip + header->code_size;
2148 mono_jit_stats.cil_code_size += header->code_size;
2150 if (cfg->method == method) {
2152 bbhash = cfg->bb_hash;
2154 real_offset = inline_offset;
2155 bbhash = g_hash_table_new (g_direct_hash, NULL);
2158 dont_inline = g_list_prepend (dont_inline, method);
2159 if (cfg->method == method) {
2162 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2163 start_bblock->cil_code = NULL;
2164 start_bblock->cil_length = 0;
2165 start_bblock->block_num = cfg->num_bblocks++;
2168 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2169 end_bblock->cil_code = NULL;
2170 end_bblock->cil_length = 0;
2171 end_bblock->block_num = cfg->num_bblocks++;
2172 g_assert (cfg->num_bblocks == 2);
2174 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2175 for (i = sig->hasthis + sig->param_count - 1; i >= 0; i--)
2176 arg_array [i] = cfg->varinfo [i];
2178 if (mono_compile_aot)
2179 cfg->opt |= MONO_OPT_SHARED;
2181 if (header->num_clauses) {
2182 int size = sizeof (int) * header->num_clauses;
2183 filter_lengths = alloca (size);
2184 memset (filter_lengths, 0, size);
2186 /* handle exception clauses */
2187 for (i = 0; i < header->num_clauses; ++i) {
2188 //unsigned char *p = ip;
2189 MonoExceptionClause *clause = &header->clauses [i];
2190 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2191 tblock->real_offset = clause->try_offset;
2192 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2193 tblock->real_offset = clause->handler_offset;
2194 /*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);
2196 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2198 /* catch and filter blocks get the exception object on the stack */
2199 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2200 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2201 /* mostly like handle_stack_args (), but just sets the input args */
2202 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2204 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2205 /* prevent it from being register allocated */
2206 cfg->exvar->flags |= MONO_INST_INDIRECT;
2208 tblock->in_scount = 1;
2209 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2210 tblock->in_stack [0] = cfg->exvar;
2211 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2212 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2213 tblock->real_offset = clause->token_or_filter;
2214 tblock->in_scount = 1;
2215 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2216 tblock->in_stack [0] = cfg->exvar;
2222 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2223 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2226 /* FIRST CODE BLOCK */
2227 bblock = NEW_BBLOCK (cfg);
2228 bblock->cil_code = ip;
2230 ADD_BBLOCK (cfg, bbhash, bblock);
2232 if (cfg->method == method) {
2233 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2234 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2235 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2236 MONO_ADD_INS (bblock, ins);
2240 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED)))) {
2241 /* we use a separate basic block for the initialization code */
2242 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2243 init_localsbb->real_offset = real_offset;
2244 start_bblock->next_bb = init_localsbb;
2245 init_localsbb->next_bb = bblock;
2246 link_bblock (cfg, start_bblock, init_localsbb);
2247 link_bblock (cfg, init_localsbb, bblock);
2248 init_localsbb->block_num = cfg->num_bblocks++;
2250 start_bblock->next_bb = bblock;
2251 link_bblock (cfg, start_bblock, bblock);
2254 mono_debug_init_method (cfg, bblock, breakpoint_id);
2256 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * (sig->hasthis + sig->param_count));
2258 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2259 for (n = 0; n < sig->param_count; ++n)
2260 param_types [n + sig->hasthis] = sig->params [n];
2262 /* do this somewhere outside - not here */
2263 NEW_ICONST (cfg, zero_int32, 0);
2264 NEW_ICONST (cfg, zero_int64, 0);
2265 zero_int64->type = STACK_I8;
2266 NEW_PCONST (cfg, zero_ptr, 0);
2267 NEW_PCONST (cfg, zero_obj, 0);
2268 zero_obj->type = STACK_OBJ;
2270 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2271 zero_r8->type = STACK_R8;
2272 zero_r8->inst_p0 = &r8_0;
2274 /* add a check for this != NULL to inlined methods */
2275 if (is_virtual_call) {
2276 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2277 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2279 MONO_ADD_INS (bblock, ins);
2282 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2283 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2286 start_new_bblock = 0;
2289 if (cfg->method == method)
2290 real_offset = ip - header->code;
2292 real_offset = inline_offset;
2294 if (start_new_bblock) {
2295 bblock->cil_length = ip - bblock->cil_code;
2296 if (start_new_bblock == 2) {
2297 g_assert (ip == tblock->cil_code);
2299 GET_BBLOCK (cfg, bbhash, tblock, ip);
2301 bblock->next_bb = tblock;
2303 start_new_bblock = 0;
2304 for (i = 0; i < bblock->in_scount; ++i) {
2305 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2309 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2310 link_bblock (cfg, bblock, tblock);
2311 if (sp != stack_start) {
2312 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2315 bblock->next_bb = tblock;
2317 for (i = 0; i < bblock->in_scount; ++i) {
2318 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2324 if (cfg->verbose_level > 3)
2325 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2332 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2333 ins->cil_code = ip++;
2334 MONO_ADD_INS (bblock, ins);
2340 CHECK_STACK_OVF (1);
2341 n = (*ip)-CEE_LDARG_0;
2342 NEW_ARGLOAD (cfg, ins, n);
2343 ins->cil_code = ip++;
2350 CHECK_STACK_OVF (1);
2351 n = (*ip)-CEE_LDLOC_0;
2352 NEW_LOCLOAD (cfg, ins, n);
2353 ins->cil_code = ip++;
2361 n = (*ip)-CEE_STLOC_0;
2363 handle_loaded_temps (cfg, bblock, stack_start, sp);
2364 NEW_LOCSTORE (cfg, ins, n, *sp);
2366 if (ins->opcode == CEE_STOBJ) {
2367 NEW_LOCLOADA (cfg, ins, n);
2368 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2370 MONO_ADD_INS (bblock, ins);
2375 CHECK_STACK_OVF (1);
2376 NEW_ARGLOAD (cfg, ins, ip [1]);
2382 CHECK_STACK_OVF (1);
2383 NEW_ARGLOADA (cfg, ins, ip [1]);
2391 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2392 handle_loaded_temps (cfg, bblock, stack_start, sp);
2394 if (ins->opcode == CEE_STOBJ) {
2395 NEW_ARGLOADA (cfg, ins, ip [1]);
2396 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2398 MONO_ADD_INS (bblock, ins);
2402 CHECK_STACK_OVF (1);
2403 NEW_LOCLOAD (cfg, ins, ip [1]);
2409 CHECK_STACK_OVF (1);
2410 NEW_LOCLOADA (cfg, ins, ip [1]);
2418 handle_loaded_temps (cfg, bblock, stack_start, sp);
2419 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
2421 if (ins->opcode == CEE_STOBJ) {
2422 NEW_LOCLOADA (cfg, ins, ip [1]);
2423 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2425 MONO_ADD_INS (bblock, ins);
2430 CHECK_STACK_OVF (1);
2431 NEW_PCONST (cfg, ins, NULL);
2433 ins->type = STACK_OBJ;
2438 CHECK_STACK_OVF (1);
2439 NEW_ICONST (cfg, ins, -1);
2453 CHECK_STACK_OVF (1);
2454 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
2460 CHECK_STACK_OVF (1);
2462 NEW_ICONST (cfg, ins, *((signed char*)ip));
2468 CHECK_STACK_OVF (1);
2469 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
2475 CHECK_STACK_OVF (1);
2476 MONO_INST_NEW (cfg, ins, OP_I8CONST);
2478 ins->type = STACK_I8;
2480 ins->inst_l = (gint64)read64 (ip);
2485 float *f = g_malloc (sizeof (float));
2486 CHECK_STACK_OVF (1);
2487 MONO_INST_NEW (cfg, ins, OP_R4CONST);
2488 ins->type = STACK_R8;
2497 double *d = g_malloc (sizeof (double));
2498 CHECK_STACK_OVF (1);
2499 MONO_INST_NEW (cfg, ins, OP_R8CONST);
2500 ins->type = STACK_R8;
2509 MonoInst *temp, *store;
2511 CHECK_STACK_OVF (1);
2516 * small optimization: if the loaded value was from a local already,
2517 * just load it twice.
2519 if (ins->ssa_op == MONO_SSA_LOAD &&
2520 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
2522 MONO_INST_NEW (cfg, temp, 0);
2524 temp->cil_code = ip;
2527 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2528 temp->cil_code = ip;
2529 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2530 store->cil_code = ip;
2531 MONO_ADD_INS (bblock, store);
2532 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2535 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2545 MONO_INST_NEW (cfg, ins, CEE_POP);
2546 MONO_ADD_INS (bblock, ins);
2547 ins->cil_code = ip++;
2552 if (stack_start != sp)
2554 MONO_INST_NEW (cfg, ins, CEE_JMP);
2555 token = read32 (ip + 1);
2556 /* FIXME: check the signature matches */
2557 cmethod = mono_get_method (image, token, NULL);
2559 * The current magic trampoline can't handle this
2560 * apparently, so we compile the method right away.
2561 * Later, we may need to fix the trampoline or use a different one.
2563 ins->inst_p0 = mono_compile_method (cmethod);
2564 MONO_ADD_INS (bblock, ins);
2566 start_new_bblock = 1;
2570 case CEE_CALLVIRT: {
2571 MonoInst *addr = NULL;
2572 MonoMethodSignature *fsig = NULL;
2573 int temp, array_rank = 0;
2574 int virtual = *ip == CEE_CALLVIRT;
2576 token = read32 (ip + 1);
2578 if (*ip == CEE_CALLI) {
2583 if (method->wrapper_type != MONO_WRAPPER_NONE)
2584 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
2586 fsig = mono_metadata_parse_signature (image, token);
2588 n = fsig->param_count + fsig->hasthis;
2591 cmethod = mono_get_method (image, token, NULL);
2593 if (!cmethod->klass->inited)
2594 mono_class_init (cmethod->klass);
2596 if (cmethod->signature->pinvoke) {
2597 #ifdef MONO_USE_EXC_TABLES
2598 if (mono_method_blittable (cmethod)) {
2599 fsig = cmethod->signature;
2602 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
2603 fsig = wrapper->signature;
2604 #ifdef MONO_USE_EXC_TABLES
2608 fsig = cmethod->signature;
2611 n = fsig->param_count + fsig->hasthis;
2613 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
2614 cmethod->klass->parent == mono_defaults.array_class) {
2615 array_rank = cmethod->klass->rank;
2618 if (cmethod->string_ctor)
2619 g_assert_not_reached ();
2625 //g_assert (!virtual || fsig->hasthis);
2629 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
2632 if (MONO_TYPE_IS_VOID (fsig->ret)) {
2633 MONO_ADD_INS (bblock, ins);
2635 type_to_eval_stack_type (fsig->ret, ins);
2644 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
2645 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
2646 mono_method_check_inlining (cmethod) &&
2647 !g_list_find (dont_inline, cmethod)) {
2649 MonoBasicBlock *ebblock;
2651 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
2655 GET_BBLOCK (cfg, bbhash, bblock, ip);
2656 ebblock->next_bb = bblock;
2657 link_bblock (cfg, ebblock, bblock);
2658 if (!MONO_TYPE_IS_VOID (fsig->ret))
2661 if (sp != stack_start) {
2662 handle_stack_args (cfg, ebblock, stack_start, sp - stack_start);
2665 start_new_bblock = 1;
2667 inline_costs += costs;
2672 inline_costs += 10 * num_calls++;
2673 handle_loaded_temps (cfg, bblock, stack_start, sp);
2675 /* tail recursion elimination */
2676 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == cfg->method && ip [5] == CEE_RET) {
2677 gboolean has_vtargs = FALSE;
2680 /* keep it simple */
2681 for (i = fsig->param_count - 1; i >= 0; i--) {
2682 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
2687 for (i = 0; i < n; ++i) {
2688 NEW_ARGSTORE (cfg, ins, i, sp [i]);
2690 MONO_ADD_INS (bblock, ins);
2692 MONO_INST_NEW (cfg, ins, CEE_BR);
2694 MONO_ADD_INS (bblock, ins);
2695 tblock = start_bblock->out_bb [0];
2696 link_bblock (cfg, bblock, tblock);
2697 ins->inst_target_bb = tblock;
2698 start_new_bblock = 1;
2701 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2702 /* just create a dummy - the value is never used */
2703 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2704 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
2712 if (*ip == CEE_CALLI) {
2714 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
2715 NEW_TEMPLOAD (cfg, *sp, temp);
2719 } else if (array_rank) {
2720 MonoMethodSignature *esig;
2723 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
2724 esig = mono_get_element_address_signature (fsig->param_count - 1);
2726 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2727 NEW_TEMPLOAD (cfg, addr, temp);
2728 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
2730 if (ins->opcode == CEE_STOBJ) {
2731 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
2733 MONO_ADD_INS (bblock, ins);
2736 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
2737 esig = mono_get_element_address_signature (fsig->param_count);
2739 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2740 NEW_TEMPLOAD (cfg, addr, temp);
2741 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
2745 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
2747 esig = mono_get_element_address_signature (fsig->param_count);
2749 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2750 NEW_TEMPLOAD (cfg, *sp, temp);
2753 g_assert_not_reached ();
2757 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
2758 /* no need to spill */
2759 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
2762 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, sp, ip, virtual ? sp [0] : NULL)) != -1) {
2763 NEW_TEMPLOAD (cfg, *sp, temp);
2773 if (cfg->method != method) {
2774 /* return from inlined methode */
2779 //g_assert (returnvar != -1);
2780 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
2781 store->cil_code = sp [0]->cil_code;
2782 if (store->opcode == CEE_STOBJ) {
2783 g_assert_not_reached ();
2784 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
2785 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
2787 MONO_ADD_INS (bblock, store);
2791 g_assert (!return_var);
2794 MONO_INST_NEW (cfg, ins, CEE_NOP);
2795 ins->opcode = mono_type_to_stind (method->signature->ret);
2796 if (ins->opcode == CEE_STOBJ) {
2797 NEW_RETLOADA (cfg, ins);
2798 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2800 ins->opcode = OP_SETRET;
2802 ins->inst_i0 = *sp;;
2803 ins->inst_i1 = NULL;
2804 MONO_ADD_INS (bblock, ins);
2808 if (sp != stack_start)
2810 MONO_INST_NEW (cfg, ins, CEE_BR);
2811 ins->cil_code = ip++;
2812 ins->inst_target_bb = end_bblock;
2813 MONO_ADD_INS (bblock, ins);
2814 link_bblock (cfg, bblock, end_bblock);
2815 start_new_bblock = 1;
2818 MONO_INST_NEW (cfg, ins, CEE_BR);
2819 ins->cil_code = ip++;
2820 MONO_ADD_INS (bblock, ins);
2821 target = ip + 1 + (signed char)(*ip);
2823 GET_BBLOCK (cfg, bbhash, tblock, target);
2824 link_bblock (cfg, bblock, tblock);
2825 CHECK_BBLOCK (target, ip, tblock);
2826 ins->inst_target_bb = tblock;
2827 if (sp != stack_start) {
2828 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2831 start_new_bblock = 1;
2837 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
2838 ins->cil_code = ip++;
2839 target = ip + 1 + *(signed char*)ip;
2841 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
2842 if (sp != stack_start) {
2843 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2859 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
2860 ins->cil_code = ip++;
2861 target = ip + 1 + *(signed char*)ip;
2864 if (sp != stack_start) {
2865 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2871 MONO_INST_NEW (cfg, ins, CEE_BR);
2872 ins->cil_code = ip++;
2873 MONO_ADD_INS (bblock, ins);
2874 target = ip + 4 + (gint32)read32(ip);
2876 GET_BBLOCK (cfg, bbhash, tblock, target);
2877 link_bblock (cfg, bblock, tblock);
2878 CHECK_BBLOCK (target, ip, tblock);
2879 ins->inst_target_bb = tblock;
2880 if (sp != stack_start) {
2881 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2884 start_new_bblock = 1;
2890 MONO_INST_NEW (cfg, ins, *ip);
2891 ins->cil_code = ip++;
2892 target = ip + 4 + (gint32)read32(ip);
2894 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
2895 if (sp != stack_start) {
2896 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2912 MONO_INST_NEW (cfg, ins, *ip);
2913 ins->cil_code = ip++;
2914 target = ip + 4 + (gint32)read32(ip);
2917 if (sp != stack_start) {
2918 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2925 n = read32 (ip + 1);
2926 MONO_INST_NEW (cfg, ins, *ip);
2928 ins->inst_left = *sp;
2929 if (ins->inst_left->type != STACK_I4) goto unverified;
2932 target = ip + n * sizeof (guint32);
2933 MONO_ADD_INS (bblock, ins);
2934 GET_BBLOCK (cfg, bbhash, tblock, target);
2935 link_bblock (cfg, bblock, tblock);
2936 ins->klass = GUINT_TO_POINTER (n);
2937 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
2938 ins->inst_many_bb [n] = tblock;
2940 for (i = 0; i < n; ++i) {
2941 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
2942 link_bblock (cfg, bblock, tblock);
2943 ins->inst_many_bb [i] = tblock;
2946 /* FIXME: handle stack args */
2961 MONO_INST_NEW (cfg, ins, *ip);
2966 ins->type = ldind_type [*ip - CEE_LDIND_I1];
2967 ins->flags |= ins_flag;
2979 MONO_INST_NEW (cfg, ins, *ip);
2980 ins->cil_code = ip++;
2982 handle_loaded_temps (cfg, bblock, stack_start, sp);
2983 MONO_ADD_INS (bblock, ins);
2984 ins->inst_i0 = sp [0];
2985 ins->inst_i1 = sp [1];
2986 ins->flags |= ins_flag;
3017 case CEE_CONV_OVF_I8:
3018 case CEE_CONV_OVF_U8:
3024 case CEE_CONV_OVF_I4:
3025 case CEE_CONV_OVF_I1:
3026 case CEE_CONV_OVF_I2:
3027 case CEE_CONV_OVF_I:
3028 case CEE_CONV_OVF_U:
3031 if (sp [-1]->type == STACK_R8) {
3032 ADD_UNOP (CEE_CONV_OVF_I8);
3040 case CEE_CONV_OVF_U1:
3041 case CEE_CONV_OVF_U2:
3042 case CEE_CONV_OVF_U4:
3045 if (sp [-1]->type == STACK_R8) {
3046 ADD_UNOP (CEE_CONV_OVF_U8);
3054 case CEE_CONV_OVF_I1_UN:
3055 case CEE_CONV_OVF_I2_UN:
3056 case CEE_CONV_OVF_I4_UN:
3057 case CEE_CONV_OVF_I8_UN:
3058 case CEE_CONV_OVF_U1_UN:
3059 case CEE_CONV_OVF_U2_UN:
3060 case CEE_CONV_OVF_U4_UN:
3061 case CEE_CONV_OVF_U8_UN:
3062 case CEE_CONV_OVF_I_UN:
3063 case CEE_CONV_OVF_U_UN:
3069 g_error ("opcode 0x%02x not handled", *ip);
3072 MonoInst *iargs [3];
3075 token = read32 (ip + 1);
3076 if (method->wrapper_type != MONO_WRAPPER_NONE)
3077 klass = mono_method_get_wrapper_data (method, token);
3079 klass = mono_class_get (image, token);
3081 mono_class_init (klass);
3082 n = mono_class_value_size (klass, NULL);
3083 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3084 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3086 NEW_ICONST (cfg, iargs [2], n);
3087 iargs [2]->cil_code = ip;
3089 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3090 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3097 CHECK_STACK_OVF (1);
3098 n = read32 (ip + 1);
3100 if (mono_compile_aot) {
3101 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3104 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
3106 MonoInst *iargs [3];
3107 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3108 NEW_IMAGECONST (cfg, iargs [1], image);
3109 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3110 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3111 NEW_TEMPLOAD (cfg, *sp, temp);
3112 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3114 NEW_PCONST (cfg, ins, NULL);
3116 ins->type = STACK_OBJ;
3117 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3124 MonoInst *iargs [2];
3127 token = read32 (ip + 1);
3128 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3129 cmethod = mono_method_get_wrapper_data (method, token);
3131 cmethod = mono_get_method (image, token, NULL);
3133 mono_class_init (cmethod->klass);
3135 n = cmethod->signature->param_count;
3138 /* move the args to allow room for 'this' in the first position */
3144 handle_loaded_temps (cfg, bblock, stack_start, sp);
3147 if (cmethod->klass->parent == mono_defaults.array_class) {
3148 NEW_METHODCONST (cfg, *sp, cmethod);
3149 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, cmethod->signature, sp, ip, FALSE);
3151 } else if (cmethod->string_ctor) {
3152 /* we simply pass a null pointer */
3153 NEW_PCONST (cfg, *sp, NULL);
3154 /* now call the string ctor */
3155 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, sp, ip, NULL);
3157 if (cmethod->klass->valuetype) {
3158 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3159 temp = iargs [0]->inst_c0;
3160 NEW_TEMPLOADA (cfg, *sp, temp);
3162 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
3163 NEW_DOMAINCONST (cfg, iargs [0]);
3164 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3166 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3168 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3169 NEW_PCONST (cfg, iargs [0], vtable);
3170 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3172 NEW_TEMPLOAD (cfg, *sp, temp);
3175 /* FIXME: currently disabled because of bug #42175 */
3176 if (0 && (cfg->opt & MONO_OPT_INLINE) && cmethod &&
3177 mono_method_check_inlining (cmethod) &&
3178 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
3179 !g_list_find (dont_inline, cmethod)) {
3181 MonoBasicBlock *ebblock;
3182 if ((costs = inline_method (cfg, cmethod, cmethod->signature, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3184 GET_BBLOCK (cfg, bbhash, bblock, ip + 5);
3185 ebblock->next_bb = bblock;
3186 link_bblock (cfg, ebblock, bblock);
3188 /*if (sp != stack_start) {
3189 handle_stack_args (cfg, ebblock, stack_start, sp - stack_start);
3192 start_new_bblock = 1;*/
3194 inline_costs += costs;
3195 /*g_print ("inlined newobj for %s\n", cmethod->klass->name);*/
3198 /* now call the actual ctor */
3199 mono_emit_method_call_spilled (cfg, bblock, cmethod, sp, ip, NULL);
3203 NEW_TEMPLOAD (cfg, *sp, temp);
3212 MONO_INST_NEW (cfg, ins, *ip);
3214 klass = mono_class_get (image, read32 (ip + 1));
3215 mono_class_init (klass);
3216 ins->type = STACK_OBJ;
3217 ins->inst_left = *sp;
3218 ins->inst_newa_class = klass;
3224 MonoInst *add, *vtoffset;
3228 token = read32 (ip + 1);
3229 if (method->wrapper_type != MONO_WRAPPER_NONE)
3230 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3232 klass = mono_class_get (image, token);
3233 mono_class_init (klass);
3236 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
3237 ins->type = STACK_OBJ;
3238 ins->inst_left = *sp;
3240 ins->inst_newa_class = klass;
3243 MONO_INST_NEW (cfg, add, CEE_ADD);
3244 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3245 add->inst_left = ins;
3246 add->inst_right = vtoffset;
3247 add->type = STACK_MP;
3255 MONO_INST_NEW (cfg, ins, *ip);
3257 klass = mono_class_get (image, read32 (ip + 1));
3258 mono_class_init (klass);
3259 ins->type = STACK_OBJ;
3260 ins->inst_left = *sp;
3262 ins->inst_newa_class = klass;
3269 MONO_INST_NEW (cfg, ins, *ip);
3271 ins->inst_left = *sp;
3272 ins->cil_code = ip++;
3273 MONO_ADD_INS (bblock, ins);
3275 start_new_bblock = 1;
3280 MonoInst *offset_ins;
3281 MonoClassField *field;
3284 if (*ip == CEE_STFLD) {
3291 // FIXME: enable this test later.
3292 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
3294 token = read32 (ip + 1);
3295 field = mono_field_from_token (image, token, &klass);
3296 mono_class_init (klass);
3297 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
3298 /* FIXME: mark instructions for use in SSA */
3299 if (*ip == CEE_STFLD) {
3300 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3301 /* fixme: we need to inline that call somehow */
3302 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
3303 MonoInst *iargs [5];
3305 NEW_CLASSCONST (cfg, iargs [1], klass);
3306 NEW_FIELDCONST (cfg, iargs [2], field);
3307 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
3309 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, iargs, ip, NULL);
3312 NEW_ICONST (cfg, offset_ins, foffset);
3313 MONO_INST_NEW (cfg, ins, CEE_ADD);
3315 ins->inst_left = *sp;
3316 ins->inst_right = offset_ins;
3317 ins->type = STACK_MP;
3319 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3320 store->cil_code = ip;
3321 store->inst_left = ins;
3322 store->inst_right = sp [1];
3323 handle_loaded_temps (cfg, bblock, stack_start, sp);
3324 store->flags |= ins_flag;
3326 if (store->opcode == CEE_STOBJ) {
3327 handle_stobj (cfg, bblock, ins, sp [1], ip,
3328 mono_class_from_mono_type (field->type), FALSE, FALSE);
3330 MONO_ADD_INS (bblock, store);
3333 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3334 /* fixme: we need to inline that call somehow */
3335 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
3336 MonoInst *iargs [4];
3339 NEW_CLASSCONST (cfg, iargs [1], klass);
3340 NEW_FIELDCONST (cfg, iargs [2], field);
3341 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
3342 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, iargs, ip, NULL);
3343 if (*ip == CEE_LDFLDA) {
3344 /* not sure howto handle this */
3345 NEW_TEMPLOADA (cfg, *sp, temp);
3347 NEW_TEMPLOAD (cfg, *sp, temp);
3351 NEW_ICONST (cfg, offset_ins, foffset);
3352 MONO_INST_NEW (cfg, ins, CEE_ADD);
3354 ins->inst_left = *sp;
3355 ins->inst_right = offset_ins;
3356 ins->type = STACK_MP;
3358 if (*ip == CEE_LDFLDA) {
3362 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3363 type_to_eval_stack_type (field->type, load);
3364 load->cil_code = ip;
3365 load->inst_left = ins;
3366 load->flags |= ins_flag;
3378 MonoClassField *field;
3381 token = read32 (ip + 1);
3383 field = mono_field_from_token (image, token, &klass);
3384 mono_class_init (klass);
3386 handle_loaded_temps (cfg, bblock, stack_start, sp);
3388 if (((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)) {
3390 MonoInst *iargs [2];
3391 g_assert (field->parent);
3392 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3393 NEW_FIELDCONST (cfg, iargs [1], field);
3394 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
3395 NEW_TEMPLOAD (cfg, ins, temp);
3398 vtable = mono_class_vtable (cfg->domain, klass);
3399 if (!cfg->domain->thread_static_fields || !(addr = g_hash_table_lookup (cfg->domain->thread_static_fields, field))) {
3400 addr = (char*)vtable->data + field->offset;
3401 NEW_PCONST (cfg, ins, addr);
3405 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
3406 * This could be later optimized to do just a couple of
3407 * memory dereferences with constant offsets.
3410 MonoInst *iargs [1];
3411 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
3412 temp = mono_emit_jit_icall (cfg, bblock, mono_threads_get_static_data, iargs, ip);
3413 NEW_TEMPLOAD (cfg, ins, temp);
3417 /* FIXME: mark instructions for use in SSA */
3418 if (*ip == CEE_LDSFLDA) {
3420 } else if (*ip == CEE_STSFLD) {
3424 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3425 store->cil_code = ip;
3426 store->inst_left = ins;
3427 store->inst_right = sp [0];
3428 store->flags |= ins_flag;
3431 if (store->opcode == CEE_STOBJ) {
3432 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
3434 MONO_ADD_INS (bblock, store);
3437 CHECK_STACK_OVF (1);
3438 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3439 type_to_eval_stack_type (field->type, load);
3440 load->cil_code = ip;
3441 load->inst_left = ins;
3443 load->flags |= ins_flag;
3445 /* fixme: dont see the problem why this does not work */
3446 //cfg->disable_aot = TRUE;
3454 token = read32 (ip + 1);
3455 if (method->wrapper_type != MONO_WRAPPER_NONE)
3456 klass = mono_method_get_wrapper_data (method, token);
3458 klass = mono_class_get (image, token);
3459 mono_class_init (klass);
3460 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
3465 MonoInst *iargs [2];
3466 MonoInst *load, *vtoffset, *add, *val, *vstore;
3471 token = read32 (ip + 1);
3472 if (method->wrapper_type != MONO_WRAPPER_NONE)
3473 klass = mono_method_get_wrapper_data (method, token);
3475 klass = mono_class_get (image, token);
3476 mono_class_init (klass);
3478 /* much like NEWOBJ */
3479 NEW_DOMAINCONST (cfg, iargs [0]);
3480 NEW_CLASSCONST (cfg, iargs [1], klass);
3482 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3483 NEW_TEMPLOAD (cfg, load, temp);
3484 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3485 MONO_INST_NEW (cfg, add, CEE_ADD);
3486 add->inst_left = load;
3487 add->inst_right = vtoffset;
3490 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
3491 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
3492 vstore->cil_code = ip;
3493 vstore->inst_left = add;
3494 vstore->inst_right = val;
3496 if (vstore->opcode == CEE_STOBJ) {
3497 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
3499 MONO_ADD_INS (bblock, vstore);
3501 NEW_TEMPLOAD (cfg, load, temp);
3509 MONO_INST_NEW (cfg, ins, *ip);
3513 token = read32 (ip + 1);
3515 /* allocate the domainvar - becaus this is used in decompose_foreach */
3516 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)
3517 mono_get_domainvar (cfg);
3519 if (method->wrapper_type != MONO_WRAPPER_NONE)
3520 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3522 klass = mono_class_get (image, token);
3524 mono_class_init (klass);
3525 ins->inst_newa_class = klass;
3526 ins->inst_newa_len = *sp;
3527 ins->type = STACK_OBJ;
3534 MONO_INST_NEW (cfg, ins, *ip);
3535 ins->cil_code = ip++;
3537 ins->inst_left = *sp;
3538 ins->type = STACK_PTR;
3544 klass = mono_class_get (image, read32 (ip + 1));
3545 mono_class_init (klass);
3546 NEW_LDELEMA (cfg, ins, sp, klass);
3561 case CEE_LDELEM_REF: {
3565 * ldind.x (ldelema (array, index))
3566 * ldelema does the bounds check
3570 klass = array_access_to_klass (*ip);
3571 NEW_LDELEMA (cfg, load, sp, klass);
3572 load->cil_code = ip;
3573 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
3575 ins->inst_left = load;
3577 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
3587 case CEE_STELEM_R8: {
3591 * stind.x (ldelema (array, index), val)
3592 * ldelema does the bounds check
3596 klass = array_access_to_klass (*ip);
3597 NEW_LDELEMA (cfg, load, sp, klass);
3598 load->cil_code = ip;
3599 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
3601 ins->inst_left = load;
3602 ins->inst_right = sp [2];
3604 handle_loaded_temps (cfg, bblock, stack_start, sp);
3605 MONO_ADD_INS (bblock, ins);
3606 /* FIXME: add the implicit STELEM_REF castclass */
3608 cfg->disable_ssa = TRUE;
3611 case CEE_STELEM_REF: {
3612 MonoInst *iargs [3];
3617 handle_loaded_temps (cfg, bblock, stack_start, sp);
3623 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
3627 NEW_GROUP (cfg, group, sp [0], sp [1]);
3628 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
3630 ins->inst_left = group;
3631 ins->inst_right = sp [2];
3632 MONO_ADD_INS (bblock, ins);
3637 cfg->disable_ssa = TRUE;
3640 case CEE_CKFINITE: {
3641 MonoInst *store, *temp;
3644 /* this instr. can throw exceptions as side effect,
3645 * so we cant eliminate dead code which contains CKFINITE opdodes.
3646 * Spilling to memory makes sure that we always perform
3650 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
3652 ins->inst_left = sp [-1];
3653 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
3655 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3656 store->cil_code = ip;
3657 MONO_ADD_INS (bblock, store);
3659 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
3666 g_error ("opcode 0x%02x not handled", *ip);
3670 MonoClass *handle_class;
3672 CHECK_STACK_OVF (1);
3674 n = read32 (ip + 1);
3676 handle = mono_ldtoken (image, n, &handle_class);
3677 mono_class_init (handle_class);
3679 if (((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)) {
3681 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
3683 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
3685 NEW_IMAGECONST (cfg, iargs [0], image);
3686 NEW_ICONST (cfg, iargs [1], n);
3687 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
3688 NEW_TEMPLOAD (cfg, res, temp);
3689 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
3690 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
3691 MONO_ADD_INS (bblock, store);
3692 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
3694 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method (image, read32 (ip + 6), NULL)) &&
3695 (cmethod->klass == mono_defaults.monotype_class->parent) &&
3696 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
3697 MonoClass *tclass = mono_class_from_mono_type (handle);
3698 mono_class_init (tclass);
3699 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
3700 ins->type = STACK_OBJ;
3701 ins->klass = cmethod->klass;
3704 NEW_PCONST (cfg, ins, handle);
3705 ins->type = STACK_VTYPE;
3706 ins->klass = handle_class;
3722 case CEE_ADD_OVF_UN:
3724 case CEE_MUL_OVF_UN:
3726 case CEE_SUB_OVF_UN:
3731 case CEE_ENDFINALLY:
3732 /* FIXME: check stack state */
3733 MONO_INST_NEW (cfg, ins, *ip);
3734 MONO_ADD_INS (bblock, ins);
3735 ins->cil_code = ip++;
3736 start_new_bblock = 1;
3741 if (*ip == CEE_LEAVE) {
3742 target = ip + 5 + (gint32)read32(ip + 1);
3744 target = ip + 2 + (signed char)(ip [1]);
3747 /* empty the stack */
3748 while (sp != stack_start) {
3749 MONO_INST_NEW (cfg, ins, CEE_POP);
3753 MONO_ADD_INS (bblock, ins);
3756 /* fixme: call fault handler ? */
3758 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
3760 for (tmp = handlers; tmp; tmp = tmp->next) {
3762 link_bblock (cfg, bblock, tblock);
3763 MONO_INST_NEW (cfg, ins, OP_HANDLER);
3765 ins->inst_target_bb = tblock;
3766 MONO_ADD_INS (bblock, ins);
3768 g_list_free (handlers);
3771 MONO_INST_NEW (cfg, ins, CEE_BR);
3773 MONO_ADD_INS (bblock, ins);
3774 GET_BBLOCK (cfg, bbhash, tblock, target);
3775 link_bblock (cfg, bblock, tblock);
3776 CHECK_BBLOCK (target, ip, tblock);
3777 ins->inst_target_bb = tblock;
3778 start_new_bblock = 1;
3780 if (*ip == CEE_LEAVE)
3789 MONO_INST_NEW (cfg, ins, *ip);
3791 handle_loaded_temps (cfg, bblock, stack_start, sp);
3792 MONO_ADD_INS (bblock, ins);
3793 ins->cil_code = ip++;
3794 ins->inst_i0 = sp [0];
3795 ins->inst_i1 = sp [1];
3803 /* trampoline mono specific opcodes */
3804 case MONO_CUSTOM_PREFIX: {
3806 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
3810 case CEE_MONO_FUNC1: {
3812 gpointer func = NULL;
3817 case MONO_MARSHAL_CONV_STR_LPWSTR:
3818 func = mono_string_to_utf16;
3820 case MONO_MARSHAL_CONV_LPWSTR_STR:
3821 func = mono_string_from_utf16;
3823 case MONO_MARSHAL_CONV_LPSTR_STR:
3824 func = mono_string_new_wrapper;
3826 case MONO_MARSHAL_CONV_STR_LPTSTR:
3827 case MONO_MARSHAL_CONV_STR_LPSTR:
3828 func = mono_string_to_utf8;
3830 case MONO_MARSHAL_CONV_STR_BSTR:
3831 func = mono_string_to_bstr;
3833 case MONO_MARSHAL_CONV_STR_TBSTR:
3834 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
3835 func = mono_string_to_ansibstr;
3837 case MONO_MARSHAL_CONV_SB_LPSTR:
3838 func = mono_string_builder_to_utf8;
3840 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
3841 func = mono_array_to_savearray;
3843 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
3844 func = mono_array_to_lparray;
3846 case MONO_MARSHAL_CONV_DEL_FTN:
3847 func = mono_delegate_to_ftnptr;
3849 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
3850 func = mono_marshal_string_array;
3853 g_warning ("unknown conversion %d\n", ip [2]);
3854 g_assert_not_reached ();
3857 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
3858 NEW_TEMPLOAD (cfg, *sp, temp);
3862 inline_costs += 10 * num_calls++;
3865 case CEE_MONO_PROC2: {
3866 gpointer func = NULL;
3871 case MONO_MARSHAL_CONV_LPSTR_SB:
3872 func = mono_string_utf8_to_builder;
3874 case MONO_MARSHAL_FREE_ARRAY:
3875 func = mono_marshal_free_array;
3878 g_assert_not_reached ();
3881 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
3883 inline_costs += 10 * num_calls++;
3886 case CEE_MONO_PROC3: {
3887 gpointer func = NULL;
3892 case MONO_MARSHAL_CONV_STR_BYVALSTR:
3893 func = mono_string_to_byvalstr;
3895 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
3896 func = mono_string_to_byvalwstr;
3899 g_assert_not_reached ();
3902 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
3904 inline_costs += 10 * num_calls++;
3910 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
3912 inline_costs += 10 * num_calls++;
3914 case CEE_MONO_LDPTR:
3915 CHECK_STACK_OVF (1);
3916 token = read32 (ip + 2);
3917 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
3921 inline_costs += 10 * num_calls++;
3923 case CEE_MONO_VTADDR:
3926 MONO_INST_NEW (cfg, ins, OP_VTADDR);
3928 ins->type = STACK_MP;
3929 ins->inst_left = *sp;
3933 case CEE_MONO_NEWOBJ: {
3934 MonoInst *iargs [2];
3936 CHECK_STACK_OVF (1);
3937 token = read32 (ip + 2);
3938 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3939 mono_class_init (klass);
3940 NEW_DOMAINCONST (cfg, iargs [0]);
3941 NEW_CLASSCONST (cfg, iargs [1], klass);
3942 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3943 NEW_TEMPLOAD (cfg, *sp, temp);
3946 inline_costs += 10 * num_calls++;
3949 case CEE_MONO_OBJADDR:
3952 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
3954 ins->type = STACK_MP;
3955 ins->inst_left = *sp;
3959 case CEE_MONO_LDNATIVEOBJ:
3961 token = read32 (ip + 2);
3962 klass = mono_method_get_wrapper_data (method, token);
3963 g_assert (klass->valuetype);
3964 mono_class_init (klass);
3965 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
3969 case CEE_MONO_RETOBJ:
3970 g_assert (cfg->ret);
3971 g_assert (method->signature->pinvoke);
3975 token = read32 (ip + 2);
3976 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3978 NEW_RETLOADA (cfg, ins);
3979 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
3981 if (sp != stack_start)
3984 MONO_INST_NEW (cfg, ins, CEE_BR);
3986 ins->inst_target_bb = end_bblock;
3987 MONO_ADD_INS (bblock, ins);
3988 link_bblock (cfg, bblock, end_bblock);
3989 start_new_bblock = 1;
3993 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
4001 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4010 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
4011 MONO_INST_NEW (cfg, ins, cmp->opcode);
4013 cmp->inst_i0 = sp [0];
4014 cmp->inst_i1 = sp [1];
4018 cmp->opcode = OP_COMPARE;
4020 ins->type = STACK_I4;
4030 CHECK_STACK_OVF (1);
4031 n = read32 (ip + 2);
4032 if (method->wrapper_type != MONO_WRAPPER_NONE)
4033 cmethod = mono_method_get_wrapper_data (method, n);
4035 cmethod = mono_get_method (image, n, NULL);
4038 * We can't do this in mono_ldftn, since it is used in
4039 * the synchronized wrapper, leading to an infinite loop.
4041 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
4042 cmethod = mono_marshal_get_synchronized_wrapper (cmethod);
4045 mono_class_init (cmethod->klass);
4046 handle_loaded_temps (cfg, bblock, stack_start, sp);
4048 NEW_METHODCONST (cfg, argconst, cmethod);
4049 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
4050 NEW_TEMPLOAD (cfg, *sp, temp);
4054 inline_costs += 10 * num_calls++;
4057 case CEE_LDVIRTFTN: {
4062 n = read32 (ip + 2);
4063 if (method->wrapper_type != MONO_WRAPPER_NONE)
4064 cmethod = mono_method_get_wrapper_data (method, n);
4066 cmethod = mono_get_method (image, n, NULL);
4068 mono_class_init (cmethod->klass);
4069 handle_loaded_temps (cfg, bblock, stack_start, sp);
4073 NEW_METHODCONST (cfg, args [1], cmethod);
4074 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
4075 NEW_TEMPLOAD (cfg, *sp, temp);
4079 inline_costs += 10 * num_calls++;
4083 CHECK_STACK_OVF (1);
4084 NEW_ARGLOAD (cfg, ins, read16 (ip + 2));
4090 CHECK_STACK_OVF (1);
4091 NEW_ARGLOADA (cfg, ins, read16 (ip + 2));
4099 handle_loaded_temps (cfg, bblock, stack_start, sp);
4100 n = read16 (ip + 2);
4101 NEW_ARGSTORE (cfg, ins, n, *sp);
4103 if (ins->opcode == CEE_STOBJ) {
4104 NEW_ARGLOADA (cfg, ins, n);
4105 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4107 MONO_ADD_INS (bblock, ins);
4111 CHECK_STACK_OVF (1);
4112 NEW_LOCLOAD (cfg, ins, read16 (ip + 2));
4118 CHECK_STACK_OVF (1);
4119 NEW_LOCLOADA (cfg, ins, read16 (ip + 2));
4127 n = read16 (ip + 2);
4128 handle_loaded_temps (cfg, bblock, stack_start, sp);
4129 NEW_LOCSTORE (cfg, ins, n, *sp);
4131 if (ins->opcode == CEE_STOBJ) {
4132 NEW_LOCLOADA (cfg, ins, n);
4133 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4135 MONO_ADD_INS (bblock, ins);
4142 if (sp != stack_start)
4144 MONO_INST_NEW (cfg, ins, 256 + ip [1]);
4145 ins->inst_left = *sp;
4148 if (header->init_locals)
4149 ins->flags |= MONO_INST_INIT;
4153 /* FIXME: set init flag if locals init is set in this method */
4155 case CEE_ENDFILTER: {
4156 MonoExceptionClause *clause, *nearest;
4157 int cc, nearest_num;
4161 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
4163 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
4164 ins->inst_left = *sp;
4166 MONO_ADD_INS (bblock, ins);
4167 start_new_bblock = 1;
4171 for (cc = 0; cc < header->num_clauses; ++cc) {
4172 clause = &header->clauses [cc];
4173 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
4174 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
4180 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
4184 case CEE_UNALIGNED_:
4185 ins_flag |= MONO_INST_UNALIGNED;
4189 ins_flag |= MONO_INST_VOLATILE;
4193 ins_flag |= MONO_INST_TAILCALL;
4199 token = read32 (ip + 2);
4200 if (method->wrapper_type != MONO_WRAPPER_NONE)
4201 klass = mono_method_get_wrapper_data (method, token);
4203 klass = mono_class_get (image, token);
4204 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
4210 MonoInst *iargs [3];
4216 handle_loaded_temps (cfg, bblock, stack_start, sp);
4217 if (ip [1] == CEE_CPBLK) {
4218 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4220 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
4228 /* FIXME: check we are in a catch handler */
4229 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
4230 load->cil_code = ip;
4231 MONO_INST_NEW (cfg, ins, CEE_THROW);
4232 ins->inst_left = load;
4234 MONO_ADD_INS (bblock, ins);
4236 start_new_bblock = 1;
4241 CHECK_STACK_OVF (1);
4242 token = read32 (ip + 2);
4243 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
4244 MonoType *type = mono_type_create_from_typespec (image, token);
4245 token = mono_type_size (type, &align);
4246 mono_metadata_free_type (type);
4248 MonoClass *szclass = mono_class_get (image, token);
4249 mono_class_init (szclass);
4250 token = mono_class_value_size (szclass, &align);
4252 NEW_ICONST (cfg, ins, token);
4257 case CEE_REFANYTYPE:
4258 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4261 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4266 g_error ("opcode 0x%02x not handled", *ip);
4269 if (start_new_bblock != 1)
4272 bblock->cil_length = ip - bblock->cil_code;
4273 bblock->next_bb = end_bblock;
4274 link_bblock (cfg, bblock, end_bblock);
4276 if (cfg->method == method && cfg->domainvar) {
4280 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
4281 call->signature = helper_sig_domain_get;
4282 call->inst.type = STACK_PTR;
4283 call->fptr = mono_domain_get;
4284 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
4286 MONO_ADD_INS (init_localsbb, store);
4289 if (header->init_locals) {
4291 for (i = 0; i < header->num_locals; ++i) {
4292 int t = header->locals [i]->type;
4293 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
4294 t = header->locals [i]->data.klass->enum_basetype->type;
4295 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
4296 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
4297 NEW_ICONST (cfg, ins, 0);
4298 NEW_LOCSTORE (cfg, store, i, ins);
4299 MONO_ADD_INS (init_localsbb, store);
4300 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
4301 MONO_INST_NEW (cfg, ins, OP_I8CONST);
4302 ins->type = STACK_I8;
4304 NEW_LOCSTORE (cfg, store, i, ins);
4305 MONO_ADD_INS (init_localsbb, store);
4306 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
4307 MONO_INST_NEW (cfg, ins, OP_R8CONST);
4308 ins->type = STACK_R8;
4309 ins->inst_p0 = (void*)&r8_0;
4310 NEW_LOCSTORE (cfg, store, i, ins);
4311 MONO_ADD_INS (init_localsbb, store);
4312 } else if (t == MONO_TYPE_VALUETYPE) {
4313 NEW_LOCLOADA (cfg, ins, i);
4314 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
4317 NEW_PCONST (cfg, ins, NULL);
4318 NEW_LOCSTORE (cfg, store, i, ins);
4319 MONO_ADD_INS (init_localsbb, store);
4325 /* resolve backward branches in the middle of an existing basic block */
4326 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
4328 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
4329 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
4330 if (tblock != start_bblock) {
4332 split_bblock (cfg, tblock, bblock);
4333 l = bblock->cil_code - header->code;
4334 bblock->cil_length = tblock->cil_length - l;
4335 tblock->cil_length = l;
4337 g_print ("recheck failed.\n");
4341 /* we compute regions here, because the length of filter clauses is not known in advance.
4342 * It is computed in the CEE_ENDFILTER case in the above switch statement*/
4343 if (cfg->method == method) {
4345 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4346 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
4347 if (cfg->verbose_level > 2)
4348 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
4351 g_hash_table_destroy (bbhash);
4354 dont_inline = g_list_remove (dont_inline, method);
4355 return inline_costs;
4358 if (cfg->method != method)
4359 g_hash_table_destroy (bbhash);
4360 dont_inline = g_list_remove (dont_inline, method);
4364 if (cfg->method != method)
4365 g_hash_table_destroy (bbhash);
4366 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
4367 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
4368 dont_inline = g_list_remove (dont_inline, method);
4373 mono_print_tree (MonoInst *tree) {
4379 arity = mono_burg_arity [tree->opcode];
4381 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
4383 switch (tree->opcode) {
4385 printf ("[%d]", tree->inst_c0);
4388 printf ("[%lld]", tree->inst_l);
4391 printf ("[%f]", *(double*)tree->inst_p0);
4394 printf ("[%f]", *(float*)tree->inst_p0);
4398 printf ("[%d]", tree->inst_c0);
4401 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
4404 printf ("[%s]", mono_arch_regname (tree->dreg));
4407 printf ("[%s]", tree->inst_newa_class->name);
4408 mono_print_tree (tree->inst_newa_len);
4419 case OP_VOIDCALLVIRT: {
4420 MonoCallInst *call = (MonoCallInst*)tree;
4422 printf ("[%s]", call->method->name);
4427 printf ("[%d (", tree->inst_c0);
4428 for (i = 0; i < tree->inst_phi_args [0]; i++) {
4431 printf ("%d", tree->inst_phi_args [i + 1]);
4443 printf ("[B%d]", tree->inst_target_bb->block_num);
4453 case OP_VOIDCALL_REG:
4454 mono_print_tree (tree->inst_left);
4466 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
4467 mono_print_tree (tree->inst_left);
4471 mono_print_tree (tree->inst_left);
4473 mono_print_tree (tree->inst_right);
4483 create_helper_signature (void)
4485 /* FIXME: set call conv */
4486 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
4487 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4488 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
4489 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
4490 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
4491 helper_sig_newarr->pinvoke = 1;
4493 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
4494 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4495 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
4496 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
4497 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
4498 helper_sig_newarr_specific->pinvoke = 1;
4500 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
4501 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4502 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
4503 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
4504 helper_sig_object_new->pinvoke = 1;
4506 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
4507 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4508 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
4509 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
4510 helper_sig_object_new_specific->pinvoke = 1;
4512 /* void* mono_method_compile (MonoMethod*) */
4513 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4514 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
4515 helper_sig_compile->pinvoke = 1;
4517 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
4518 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4519 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
4520 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
4521 helper_sig_compile_virt->pinvoke = 1;
4523 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
4524 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4525 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
4526 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
4527 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
4528 helper_sig_ldstr->pinvoke = 1;
4530 /* MonoDomain *mono_domain_get (void) */
4531 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
4532 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
4533 helper_sig_domain_get->pinvoke = 1;
4535 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
4536 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4537 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
4538 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
4539 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
4540 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
4541 helper_sig_stelem_ref->pinvoke = 1;
4543 /* long amethod (long, long) */
4544 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4545 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
4546 &mono_defaults.int64_class->byval_arg;
4547 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
4548 helper_sig_long_long_long->pinvoke = 1;
4550 /* object amethod (intptr) */
4551 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4552 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4553 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
4554 helper_sig_obj_ptr->pinvoke = 1;
4556 /* void amethod (intptr) */
4557 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4558 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4559 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
4560 helper_sig_void_ptr->pinvoke = 1;
4562 /* void amethod (MonoObject *obj) */
4563 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4564 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
4565 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
4566 helper_sig_void_obj->pinvoke = 1;
4568 /* intptr amethod (void) */
4569 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
4570 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
4571 helper_sig_ptr_void->pinvoke = 1;
4573 /* void amethod (intptr, intptr) */
4574 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4575 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4576 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
4577 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
4578 helper_sig_void_ptr_ptr->pinvoke = 1;
4580 /* void amethod (intptr, intptr, intptr) */
4581 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4582 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4583 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
4584 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
4585 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
4586 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
4588 /* intptr amethod (intptr, intptr) */
4589 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4590 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4591 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
4592 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
4593 helper_sig_ptr_ptr_ptr->pinvoke = 1;
4595 /* IntPtr amethod (object) */
4596 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4597 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
4598 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
4599 helper_sig_ptr_obj->pinvoke = 1;
4601 /* IntPtr amethod (int) */
4602 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4603 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
4604 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
4605 helper_sig_ptr_int->pinvoke = 1;
4607 /* long amethod (long, guint32) */
4608 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4609 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
4610 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
4611 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
4612 helper_sig_long_long_int->pinvoke = 1;
4614 /* ulong amethod (double) */
4615 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4616 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
4617 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
4618 helper_sig_ulong_double->pinvoke = 1;
4620 /* long amethod (double) */
4621 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4622 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
4623 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
4624 helper_sig_long_double->pinvoke = 1;
4626 /* uint amethod (double) */
4627 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4628 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
4629 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
4630 helper_sig_uint_double->pinvoke = 1;
4632 /* int amethod (double) */
4633 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4634 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
4635 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
4636 helper_sig_int_double->pinvoke = 1;
4638 /* void initobj (intptr, int size) */
4639 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4640 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
4641 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
4642 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
4643 helper_sig_initobj->pinvoke = 1;
4645 /* void memcpy (intptr, intptr, int size) */
4646 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4647 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
4648 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
4649 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
4650 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
4651 helper_sig_memcpy->pinvoke = 1;
4653 /* void memset (intptr, int val, int size) */
4654 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4655 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
4656 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
4657 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
4658 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
4659 helper_sig_memset->pinvoke = 1;
4662 static GHashTable *jit_icall_hash_name = NULL;
4663 static GHashTable *jit_icall_hash_addr = NULL;
4666 mono_find_jit_icall_by_name (const char *name)
4668 g_assert (jit_icall_hash_name);
4670 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
4671 return g_hash_table_lookup (jit_icall_hash_name, name);
4675 mono_find_jit_icall_by_addr (gconstpointer addr)
4677 g_assert (jit_icall_hash_addr);
4679 return g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
4683 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
4685 MonoJitICallInfo *info;
4686 MonoMethod *wrapper;
4692 if (!jit_icall_hash_name) {
4693 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
4694 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
4697 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
4698 g_warning ("jit icall already defined \"%s\"\n", name);
4699 g_assert_not_reached ();
4702 info = g_new (MonoJitICallInfo, 1);
4704 info->name = g_strdup (name);
4709 #ifdef MONO_USE_EXC_TABLES
4710 || mono_arch_has_unwind_info (func)
4713 info->wrapper = func;
4716 n = g_strdup_printf ("__icall_wrapper_%s", name);
4717 wrapper = mono_marshal_get_icall_wrapper (sig, n, func);
4718 info->wrapper = mono_jit_compile_method (wrapper);
4722 g_hash_table_insert (jit_icall_hash_name, info->name, info);
4723 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
4724 if (func != info->wrapper)
4725 g_hash_table_insert (jit_icall_hash_addr, (gpointer)info->wrapper, info);
4730 static GHashTable *emul_opcode_hash = NULL;
4732 static MonoJitICallInfo *
4733 mono_find_jit_opcode_emulation (int opcode)
4735 if (emul_opcode_hash)
4736 return g_hash_table_lookup (emul_opcode_hash, (gpointer)opcode);
4742 mono_register_opcode_emulation (int opcode, MonoMethodSignature *sig, gpointer func)
4744 MonoJitICallInfo *info;
4747 if (!emul_opcode_hash)
4748 emul_opcode_hash = g_hash_table_new (NULL, NULL);
4750 g_assert (!sig->hasthis);
4751 g_assert (sig->param_count < 3);
4753 name = g_strdup_printf ("__emulate_%s", mono_inst_name (opcode));
4755 info = mono_register_jit_icall (func, name, sig, FALSE);
4759 g_hash_table_insert (emul_opcode_hash, (gpointer)opcode, info);
4763 decompose_foreach (MonoInst *tree, gpointer data)
4765 static MonoJitICallInfo *newarr_info = NULL;
4766 static MonoJitICallInfo *newarr_specific_info = NULL;
4767 MonoJitICallInfo *info;
4769 switch (tree->opcode) {
4771 MonoCompile *cfg = data;
4772 MonoInst *iargs [3];
4775 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
4776 g_assert (newarr_info);
4777 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
4778 g_assert (newarr_specific_info);
4781 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
4782 NEW_DOMAINCONST (cfg, iargs [0]);
4783 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
4784 iargs [2] = tree->inst_newa_len;
4789 MonoVTable *vtable = mono_class_vtable (cfg->domain,
4790 mono_array_class_get (&tree->inst_newa_class->byval_arg, 1));
4792 NEW_PCONST (cfg, iargs [0], vtable);
4793 iargs [1] = tree->inst_newa_len;
4795 info = newarr_specific_info;
4798 mono_emulate_opcode (cfg, tree, iargs, info);
4808 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
4810 switch (mono_burg_arity [tree->opcode]) {
4813 mono_inst_foreach (tree->inst_left, func, data);
4816 mono_inst_foreach (tree->inst_left, func, data);
4817 mono_inst_foreach (tree->inst_right, func, data);
4820 g_assert_not_reached ();
4827 mono_print_bb_code (MonoBasicBlock *bb) {
4829 MonoInst *c = bb->code;
4831 mono_print_tree (c);
4840 print_dfn (MonoCompile *cfg) {
4845 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
4847 for (i = 0; i < cfg->num_bblocks; ++i) {
4848 bb = cfg->bblocks [i];
4850 char* code1, *code2;
4851 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
4852 if (bb->last_ins->cil_code)
4853 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
4855 code2 = g_strdup ("");
4857 code1 [strlen (code1) - 1] = 0;
4858 code = g_strdup_printf ("%s -> %s", code1, code2);
4862 code = g_strdup ("\n");
4863 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
4865 MonoInst *c = bb->code;
4867 mono_print_tree (c);
4875 g_print ("\tprev:");
4876 for (j = 0; j < bb->in_count; ++j) {
4877 g_print (" BB%d", bb->in_bb [j]->block_num);
4879 g_print ("\t\tsucc:");
4880 for (j = 0; j < bb->out_count; ++j) {
4881 g_print (" BB%d", bb->out_bb [j]->block_num);
4883 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
4886 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
4889 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
4891 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
4899 * returns the offset used by spillvar. It allocates a new
4900 * spill variable if necessary.
4903 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
4905 MonoSpillInfo **si, *info;
4908 si = &cfg->spill_info;
4910 while (i <= spillvar) {
4913 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
4915 cfg->stack_offset -= sizeof (gpointer);
4916 info->offset = cfg->stack_offset;
4920 return (*si)->offset;
4926 g_assert_not_reached ();
4931 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
4935 g_assert (bb->code);
4936 bb->last_ins->next = inst;
4937 bb->last_ins = inst;
4939 bb->last_ins = bb->code = inst;
4944 mono_destroy_compile (MonoCompile *cfg)
4946 //mono_mempool_stats (cfg->mempool);
4947 g_hash_table_destroy (cfg->bb_hash);
4949 mono_regstate_free (cfg->rs);
4950 mono_mempool_destroy (cfg->mempool);
4951 g_list_free (cfg->ldstr_list);
4953 g_free (cfg->varinfo);
4959 mono_get_lmf_addr (void)
4961 MonoJitTlsData *jit_tls;
4963 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
4964 return &jit_tls->lmf;
4966 g_assert_not_reached ();
4971 * mono_thread_abort:
4972 * @obj: exception object
4974 * abort the thread, print exception information and stack trace
4977 mono_thread_abort (MonoObject *obj)
4979 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
4987 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
4989 MonoJitTlsData *jit_tls;
4992 jit_tls = g_new0 (MonoJitTlsData, 1);
4994 TlsSetValue (mono_jit_tls_id, jit_tls);
4996 jit_tls->abort_func = mono_thread_abort;
4997 jit_tls->end_of_stack = stack_start;
4999 lmf = g_new0 (MonoLMF, 1);
5005 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
5008 mono_thread_abort_dummy (MonoObject *obj)
5010 if (mono_thread_attach_aborted_cb)
5011 mono_thread_attach_aborted_cb (obj);
5013 mono_thread_abort (obj);
5017 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
5019 MonoJitTlsData *jit_tls;
5022 jit_tls = g_new0 (MonoJitTlsData, 1);
5024 TlsSetValue (mono_jit_tls_id, jit_tls);
5026 jit_tls->abort_func = mono_thread_abort_dummy;
5027 jit_tls->end_of_stack = stack_start;
5029 lmf = g_new0 (MonoLMF, 1);
5036 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
5038 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
5042 ji->data.target = target;
5043 ji->next = cfg->patch_info;
5045 cfg->patch_info = ji;
5049 mono_remove_patch_info (MonoCompile *cfg, int ip)
5051 MonoJumpInfo **ji = &cfg->patch_info;
5054 if ((*ji)->ip.i == ip)
5057 ji = &((*ji)->next);
5062 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
5063 MonoJitICallInfo *info;
5065 switch (mono_burg_arity [tree->opcode]) {
5068 dec_foreach (tree->inst_left, cfg);
5070 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5071 MonoInst *iargs [2];
5073 iargs [0] = tree->inst_left;
5075 mono_emulate_opcode (cfg, tree, iargs, info);
5081 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5082 MonoInst *iargs [2];
5084 iargs [0] = tree->inst_i0;
5085 iargs [1] = tree->inst_i1;
5087 mono_emulate_opcode (cfg, tree, iargs, info);
5089 dec_foreach (iargs [0], cfg);
5090 dec_foreach (iargs [1], cfg);
5093 dec_foreach (tree->inst_left, cfg);
5094 dec_foreach (tree->inst_right, cfg);
5098 g_assert_not_reached ();
5100 decompose_foreach (tree, cfg);
5104 decompose_pass (MonoCompile *cfg) {
5107 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5110 cfg->prev_ins = NULL;
5111 for (tree = cfg->cbb->code; tree; tree = tree->next) {
5112 dec_foreach (tree, cfg);
5113 cfg->prev_ins = tree;
5119 nullify_basic_block (MonoBasicBlock *bb)
5126 bb->code = bb->last_ins = NULL;
5130 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
5134 for (i = 0; i < bb->out_count; i++) {
5135 MonoBasicBlock *ob = bb->out_bb [i];
5136 for (j = 0; j < ob->in_count; j++) {
5137 if (ob->in_bb [j] == orig)
5138 ob->in_bb [j] = repl;
5145 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
5147 bb->out_count = bbn->out_count;
5148 bb->out_bb = bbn->out_bb;
5150 replace_basic_block (bb, bbn, bb);
5154 bb->last_ins->next = bbn->code;
5155 bb->last_ins = bbn->last_ins;
5158 bb->code = bbn->code;
5159 bb->last_ins = bbn->last_ins;
5161 bb->next_bb = bbn->next_bb;
5162 nullify_basic_block (bbn);
5166 optimize_branches (MonoCompile *cfg) {
5167 int changed = FALSE;
5168 MonoBasicBlock *bb, *bbn;
5173 /* we skip the entry block (exit is handled specially instead ) */
5174 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
5176 /* dont touch code inside exception clauses */
5177 if (bb->region != -1)
5180 if (bb->out_count == 1) {
5181 bbn = bb->out_bb [0];
5183 if (bb->region == bbn->region && bb->next_bb == bbn) {
5184 /* the block are in sequence anyway ... */
5187 * miguel: I do not understand what the test below does, could we
5188 * use a macro, or a comment here? opcode > CEE_BEQ && <= BLT_UN
5190 * It could also test for bb->last_in only once, and the value
5191 * could be cached (last_ins->opcode)
5193 if (bb->last_ins && (bb->last_ins->opcode == CEE_BR || (
5194 (bb->last_ins && bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN)))) {
5195 bb->last_ins->opcode = CEE_NOP;
5197 if (cfg->verbose_level > 2)
5198 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
5200 /* fixme: this causes problems with inlining */
5201 if (bbn->in_count == 1) {
5203 if (bbn != cfg->bb_exit) {
5204 if (cfg->verbose_level > 2)
5205 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
5206 merge_basic_blocks (bb, bbn);
5210 //mono_print_bb_code (bb);
5213 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
5214 bbn = bb->last_ins->inst_target_bb;
5215 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR) {
5217 if (cfg->verbose_level > 2)
5218 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name, bb->block_num, bbn->block_num);
5219 bb->out_bb [0] = bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
5224 } else if (bb->out_count == 2) {
5225 /* fixme: this does not correctly unlink the blocks, so we get serious problems in idom code */
5226 if (0 && bb->last_ins && bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
5227 bbn = bb->last_ins->inst_true_bb;
5228 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR) {
5229 if (cfg->verbose_level > 2)
5230 g_print ("cbranch to branch triggered %d -> %d (0x%02x)\n", bb->block_num,
5231 bbn->block_num, bbn->code->opcode);
5233 if (bb->out_bb [0] == bbn) {
5234 bb->out_bb [0] = bbn->code->inst_target_bb;
5235 } else if (bb->out_bb [1] == bbn) {
5236 bb->out_bb [1] = bbn->code->inst_target_bb;
5238 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
5248 mono_compile_create_vars (MonoCompile *cfg)
5250 MonoMethodSignature *sig;
5251 MonoMethodHeader *header;
5254 header = ((MonoMethodNormal *)cfg->method)->header;
5256 sig = cfg->method->signature;
5258 if (!MONO_TYPE_IS_VOID (sig->ret)) {
5259 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
5260 cfg->ret->opcode = OP_RETARG;
5261 cfg->ret->inst_vtype = sig->ret;
5262 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
5266 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
5268 for (i = 0; i < sig->param_count; ++i)
5269 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
5271 cfg->locals_start = cfg->num_varinfo;
5273 for (i = 0; i < header->num_locals; ++i)
5274 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
5279 mono_print_code (MonoCompile *cfg)
5283 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5284 MonoInst *tree = bb->code;
5289 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
5291 for (; tree; tree = tree->next) {
5292 mono_print_tree (tree);
5297 bb->last_ins->next = NULL;
5302 extern const char * const mono_burg_rule_string [];
5305 emit_state (MonoCompile *cfg, MBState *state, int goal)
5308 int ern = mono_burg_rule (state, goal);
5309 const guint16 *nts = mono_burg_nts [ern];
5312 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
5316 // state->reg1 = state->reg2; /* chain rule */
5318 state->reg1 = mono_regstate_next_int (cfg->rs);
5319 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
5322 state->reg1 = mono_regstate_next_int (cfg->rs);
5323 state->reg2 = mono_regstate_next_int (cfg->rs);
5326 state->reg1 = mono_regstate_next_float (cfg->rs);
5333 mono_burg_kids (state, ern, kids);
5335 emit_state (cfg, kids [0], nts [0]);
5337 emit_state (cfg, kids [1], nts [1]);
5339 g_assert (!nts [3]);
5340 emit_state (cfg, kids [2], nts [2]);
5345 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
5346 if ((emit = mono_burg_func [ern]))
5347 emit (state, state->tree, cfg);
5350 #define DEBUG_SELECTION
5353 mini_select_instructions (MonoCompile *cfg)
5357 cfg->state_pool = mono_mempool_new ();
5358 cfg->rs = mono_regstate_new ();
5360 #ifdef DEBUG_SELECTION
5361 if (cfg->verbose_level >= 4) {
5362 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5363 MonoInst *tree = bb->code;
5364 g_print ("DUMP BLOCK %d:\n", bb->block_num);
5367 for (; tree; tree = tree->next) {
5368 mono_print_tree (tree);
5375 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5376 MonoInst *tree = bb->code, *next;
5382 bb->last_ins = NULL;
5385 mono_regstate_reset (cfg->rs);
5387 #ifdef DEBUG_SELECTION
5388 if (cfg->verbose_level >= 3)
5389 g_print ("LABEL BLOCK %d:\n", bb->block_num);
5391 for (; tree; tree = next) {
5393 #ifdef DEBUG_SELECTION
5394 if (cfg->verbose_level >= 3) {
5395 mono_print_tree (tree);
5400 if (!(mbstate = mono_burg_label (tree, cfg))) {
5401 g_warning ("unabled to label tree %p", tree);
5402 mono_print_tree (tree);
5404 g_assert_not_reached ();
5406 emit_state (cfg, mbstate, MB_NTERM_stmt);
5408 bb->max_ireg = cfg->rs->next_vireg;
5409 bb->max_freg = cfg->rs->next_vfreg;
5412 bb->last_ins->next = NULL;
5414 mono_mempool_empty (cfg->state_pool);
5416 mono_mempool_destroy (cfg->state_pool);
5420 mono_codegen (MonoCompile *cfg)
5422 MonoJumpInfo *patch_info;
5424 int i, max_epilog_size;
5427 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5428 cfg->spill_count = 0;
5429 /* we reuse dfn here */
5430 /* bb->dfn = bb_count++; */
5431 mono_arch_local_regalloc (cfg, bb);
5434 if (mono_trace_coverage)
5435 mono_allocate_coverage_info (cfg->method, cfg->num_bblocks);
5437 code = mono_arch_emit_prolog (cfg);
5439 if (mono_jit_profile)
5440 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
5442 cfg->code_len = code - cfg->native_code;
5443 cfg->prolog_end = cfg->code_len;
5445 mono_debug_open_method (cfg);
5447 /* emit code all basic blocks */
5448 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5449 bb->native_offset = cfg->code_len;
5450 mono_arch_output_basic_block (cfg, bb);
5452 cfg->bb_exit->native_offset = cfg->code_len;
5454 code = cfg->native_code + cfg->code_len;
5456 max_epilog_size = mono_arch_max_epilog_size (cfg);
5458 /* we always allocate code in cfg->domain->code_mp to increase locality */
5459 cfg->code_size = cfg->code_len + max_epilog_size;
5460 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
5461 code = mono_mempool_alloc (cfg->domain->code_mp, cfg->code_size);
5462 memcpy (code, cfg->native_code, cfg->code_len);
5463 g_free (cfg->native_code);
5464 cfg->native_code = code;
5465 code = cfg->native_code + cfg->code_len;
5467 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
5469 cfg->epilog_begin = cfg->code_len;
5471 if (mono_jit_profile)
5472 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
5474 cfg->code_len = code - cfg->native_code;
5476 mono_arch_emit_epilog (cfg);
5478 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5479 switch (patch_info->type) {
5480 case MONO_PATCH_INFO_ABS: {
5481 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
5483 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
5484 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
5485 patch_info->data.name = info->name;
5489 case MONO_PATCH_INFO_SWITCH: {
5490 gpointer *table = g_new (gpointer, patch_info->table_size);
5491 patch_info->ip.i = patch_info->ip.label->inst_c0;
5492 for (i = 0; i < patch_info->table_size; i++) {
5493 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
5495 patch_info->data.target = table;
5504 if (cfg->verbose_level > 1)
5505 g_print ("Method %s::%s emmitted at %p to %p\n", cfg->method->klass->name,
5506 cfg->method->name, cfg->native_code, cfg->native_code + cfg->code_len);
5508 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info);
5510 mono_debug_close_method (cfg);
5514 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
5519 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
5520 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
5522 if (cp->opcode == OP_ICONST) {
5523 if (cfg->opt & MONO_OPT_CONSPROP) {
5524 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
5528 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
5529 if (cfg->opt & MONO_OPT_COPYPROP) {
5530 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
5536 arity = mono_burg_arity [tree->opcode];
5539 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
5540 if (cfg->opt & MONO_OPT_CFOLD)
5541 mono_constant_fold_inst (tree, NULL);
5543 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
5544 if (cfg->opt & MONO_OPT_CFOLD)
5545 mono_constant_fold_inst (tree, NULL);
5547 mono_constant_fold_inst (tree, NULL);
5553 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
5557 switch (tree->opcode) {
5567 if (tree->ssa_op == MONO_SSA_NOP) {
5568 memset (acp, 0, sizeof (MonoInst *) * acp_size);
5585 case OP_VOIDCALL_REG:
5586 case OP_VOIDCALLVIRT:
5588 MonoCallInst *call = (MonoCallInst *)tree;
5589 MonoMethodSignature *sig = call->signature;
5590 int i, byref = FALSE;
5592 for (i = 0; i < sig->param_count; i++) {
5593 if (sig->params [i]->byref) {
5600 memset (acp, 0, sizeof (MonoInst *) * acp_size);
5608 arity = mono_burg_arity [tree->opcode];
5614 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
5617 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
5618 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
5621 g_assert_not_reached ();
5626 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
5628 MonoInst *tree = bb->code;
5634 for (; tree; tree = tree->next) {
5636 mono_cprop_copy_values (cfg, tree, acp);
5638 mono_cprop_invalidate_values (tree, acp, acp_size);
5640 if (tree->ssa_op == MONO_SSA_STORE &&
5641 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
5642 MonoInst *i1 = tree->inst_i1;
5644 acp [tree->inst_i0->inst_c0] = NULL;
5646 for (i = 0; i < acp_size; i++) {
5647 if (acp [i] && acp [i]->opcode != OP_ICONST &&
5648 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
5653 if (i1->opcode == OP_ICONST) {
5654 acp [tree->inst_i0->inst_c0] = i1;
5655 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
5657 if (i1->ssa_op == MONO_SSA_LOAD &&
5658 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
5659 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
5660 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
5661 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
5666 if (tree->opcode == CEE_BEQ) {
5667 g_assert (tree->inst_i0->opcode == OP_COMPARE);
5668 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
5669 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
5671 tree->opcode = CEE_BR;
5672 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
5673 tree->inst_target_bb = tree->inst_true_bb;
5675 tree->inst_target_bb = tree->inst_false_bb;
5684 mono_local_cprop (MonoCompile *cfg)
5689 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
5691 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5692 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
5693 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
5698 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int parts)
5700 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
5701 guint8 *ip = (guint8 *)header->code;
5704 int dfn = 0, i, code_size_ratio;
5706 mono_jit_stats.methods_compiled++;
5707 if (mono_jit_profile)
5708 mono_profiler_method_jit (method);
5710 cfg = g_new0 (MonoCompile, 1);
5711 cfg->method = method;
5712 cfg->mempool = mono_mempool_new ();
5714 cfg->bb_hash = g_hash_table_new (g_direct_hash, NULL);
5715 cfg->domain = domain;
5716 cfg->verbose_level = mini_verbose;
5719 * create MonoInst* which represents arguments and local variables
5721 mono_compile_create_vars (cfg);
5723 if (cfg->verbose_level > 2)
5724 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
5726 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
5727 mono_destroy_compile (cfg);
5728 if (mono_jit_profile)
5729 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
5733 mono_jit_stats.basic_blocks += cfg->num_bblocks;
5734 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
5736 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
5738 /* Depth-first ordering on basic blocks */
5739 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
5741 if (cfg->opt & MONO_OPT_BRANCH)
5742 optimize_branches (cfg);
5744 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
5745 if (cfg->num_bblocks != dfn + 1) {
5746 if (cfg->verbose_level > 1)
5747 g_print ("unreachable code?\n");
5748 cfg->num_bblocks = dfn + 1;
5751 if (cfg->opt & MONO_OPT_LOOP) {
5752 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
5753 mono_compute_natural_loops (cfg);
5757 /* after method_to_ir */
5761 //#define DEBUGSSA "logic_run"
5762 #define DEBUGSSA_CLASS "Tests"
5766 if (!header->num_clauses && !cfg->disable_ssa) {
5767 mono_local_cprop (cfg);
5768 mono_ssa_compute (cfg);
5772 /* fixme: add all optimizations which requires SSA */
5773 if (cfg->opt & (MONO_OPT_DEADCE)) {
5774 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
5775 mono_local_cprop (cfg);
5776 mono_ssa_compute (cfg);
5778 if (cfg->verbose_level >= 2) {
5785 /* after SSA translation */
5789 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
5790 if (cfg->comp_done & MONO_COMP_SSA) {
5791 mono_ssa_cprop (cfg);
5793 mono_local_cprop (cfg);
5797 if (cfg->comp_done & MONO_COMP_SSA) {
5798 mono_ssa_deadce (cfg);
5800 //mono_ssa_strength_reduction (cfg);
5802 mono_ssa_remove (cfg);
5804 if (cfg->opt & MONO_OPT_BRANCH)
5805 optimize_branches (cfg);
5808 /* after SSA removal */
5812 decompose_pass (cfg);
5814 /* FIXME: disabled with exception clauses: bug #42136 */
5815 if ((!header->num_clauses) && (cfg->opt & MONO_OPT_LINEARS)) {
5818 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
5819 cfg->comp_done &= ~MONO_COMP_LIVENESS;
5820 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
5821 mono_analyze_liveness (cfg);
5823 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
5824 regs = mono_arch_get_global_int_regs (cfg);
5825 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
5829 //mono_print_code (cfg);
5833 /* variables are allocated after decompose, since decompose could create temps */
5834 mono_arch_allocate_vars (cfg);
5836 if (cfg->opt & MONO_OPT_CFOLD)
5837 mono_constant_fold (cfg);
5839 mini_select_instructions (cfg);
5842 if (cfg->verbose_level >= 2) {
5843 char *id = mono_method_full_name (cfg->method, FALSE);
5844 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
5848 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
5850 jinfo = g_new0 (MonoJitInfo, 1);
5851 jinfo->method = method;
5852 jinfo->code_start = cfg->native_code;
5853 jinfo->code_size = cfg->code_len;
5854 jinfo->used_regs = cfg->used_int_regs;
5856 if (header->num_clauses) {
5859 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
5860 jinfo->num_clauses = header->num_clauses;
5861 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
5862 sizeof (MonoJitExceptionInfo) * header->num_clauses);
5864 for (i = 0; i < header->num_clauses; i++) {
5865 MonoExceptionClause *ec = &header->clauses [i];
5866 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
5867 MonoBasicBlock *tblock;
5869 ei->flags = ec->flags;
5871 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
5872 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
5874 ei->data.filter = cfg->native_code + tblock->native_offset;
5876 ei->data.token = ec->token_or_filter;
5879 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
5881 ei->try_start = cfg->native_code + tblock->native_offset;
5882 g_assert (tblock->native_offset);
5883 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
5885 ei->try_end = cfg->native_code + tblock->native_offset;
5886 g_assert (tblock->native_offset);
5887 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
5889 ei->handler_start = cfg->native_code + tblock->native_offset;
5893 mono_jit_info_table_add (cfg->domain, jinfo);
5895 /* collect statistics */
5896 mono_jit_stats.allocated_code_size += cfg->code_len;
5897 code_size_ratio = cfg->code_len;
5898 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
5899 mono_jit_stats.biggest_method_size = code_size_ratio;
5900 mono_jit_stats.biggest_method = method;
5902 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
5903 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
5904 mono_jit_stats.max_code_size_ratio = code_size_ratio;
5905 mono_jit_stats.max_ratio_method = method;
5907 mono_jit_stats.native_code_size += cfg->code_len;
5909 if (mono_jit_profile)
5910 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
5916 mono_jit_compile_method (MonoMethod *method)
5918 /* FIXME: later copy the code from mono */
5919 MonoDomain *target_domain, *domain = mono_domain_get ();
5921 GHashTable *jit_code_hash;
5924 if (default_opt & MONO_OPT_SHARED)
5925 target_domain = mono_root_domain;
5927 target_domain = domain;
5929 jit_code_hash = target_domain->jit_code_hash;
5931 if ((code = g_hash_table_lookup (jit_code_hash, method))) {
5932 mono_jit_stats.methods_lookups++;
5936 #ifdef MONO_USE_AOT_COMPILER
5937 if (!mono_compile_aot) {
5938 mono_class_init (method->klass);
5939 if ((code = mono_aot_get_method (method))) {
5940 g_hash_table_insert (jit_code_hash, method, code);
5946 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
5947 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
5948 if (!method->info) {
5951 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
5952 mono_lookup_pinvoke_call (method);
5953 #ifdef MONO_USE_EXC_TABLES
5954 if (mono_method_blittable (method)) {
5955 method->info = method->addr;
5958 nm = mono_marshal_get_native_wrapper (method);
5959 method->info = mono_compile_method (nm);
5961 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
5962 //mono_debug_add_wrapper (method, nm);
5963 #ifdef MONO_USE_EXC_TABLES
5967 return method->info;
5968 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
5969 const char *name = method->name;
5972 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
5973 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
5974 /* FIXME: uhm, we need a wrapper to handle exceptions? */
5975 return (gpointer)mono_delegate_ctor;
5976 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
5977 nm = mono_marshal_get_delegate_invoke (method);
5978 return mono_jit_compile_method (nm);
5979 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
5980 nm = mono_marshal_get_delegate_begin_invoke (method);
5981 return mono_jit_compile_method (nm);
5982 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
5983 nm = mono_marshal_get_delegate_end_invoke (method);
5984 return mono_jit_compile_method (nm);
5990 cfg = mini_method_compile (method, default_opt, target_domain, 0);
5991 code = cfg->native_code;
5992 mono_destroy_compile (cfg);
5994 g_hash_table_insert (jit_code_hash, method, code);
5996 /* make sure runtime_init is called */
5997 mono_class_vtable (target_domain, method->klass);
6003 * mono_jit_runtime_invoke:
6004 * @method: the method to invoke
6005 * @obj: this pointer
6006 * @params: array of parameter values.
6007 * @exc: used to catch exceptions objects
6010 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
6013 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
6014 invoke = mono_marshal_get_runtime_invoke (method);
6015 runtime_invoke = mono_jit_compile_method (invoke);
6016 return runtime_invoke (obj, params, exc);
6019 #ifdef PLATFORM_WIN32
6020 #define GET_CONTEXT \
6021 struct sigcontext *ctx = (struct sigcontext*)_dummy;
6023 #define GET_CONTEXT \
6024 void **_p = (void **)&_dummy; \
6025 struct sigcontext *ctx = (struct sigcontext *)++_p;
6029 sigfpe_signal_handler (int _dummy)
6034 exc = mono_get_exception_divide_by_zero ();
6036 mono_arch_handle_exception (ctx, exc, FALSE);
6040 sigill_signal_handler (int _dummy)
6044 exc = mono_get_exception_execution_engine ("SIGILL");
6046 mono_arch_handle_exception (ctx, exc, FALSE);
6050 sigsegv_signal_handler (int _dummy)
6055 exc = mono_get_exception_null_reference ();
6057 mono_arch_handle_exception (ctx, exc, FALSE);
6061 sigusr1_signal_handler (int _dummy)
6066 thread = mono_thread_current ();
6068 g_assert (thread->abort_exc);
6070 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
6074 mono_runtime_install_handlers (void)
6076 #ifndef PLATFORM_WIN32
6077 struct sigaction sa;
6080 #ifdef PLATFORM_WIN32
6082 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
6083 win32_seh_set_handler(SIGILL, sigill_signal_handler);
6084 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
6085 #else /* !PLATFORM_WIN32 */
6087 /* libpthreads has its own implementation of sigaction(),
6088 * but it seems to work well with our current exception
6089 * handlers. If not we must call syscall directly instead
6093 sa.sa_handler = sigfpe_signal_handler;
6094 sigemptyset (&sa.sa_mask);
6096 //g_assert (syscall (SYS_sigaction, SIGFPE, &sa, NULL) != -1);
6097 g_assert (sigaction (SIGFPE, &sa, NULL) != -1);
6100 sa.sa_handler = sigill_signal_handler;
6101 sigemptyset (&sa.sa_mask);
6103 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
6104 g_assert (sigaction (SIGILL, &sa, NULL) != -1);
6106 /* catch thread abort signal */
6107 sa.sa_handler = sigusr1_signal_handler;
6108 sigemptyset (&sa.sa_mask);
6110 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
6111 g_assert (sigaction (mono_thread_get_abort_signal (), &sa, NULL) != -1);
6115 sa.sa_handler = sigsegv_signal_handler;
6116 sigemptyset (&sa.sa_mask);
6118 //g_assert (syscall (SYS_sigaction, SIGSEGV, &sa, NULL) != -1);
6119 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
6121 #endif /* PLATFORM_WIN32 */
6124 /* mono_jit_create_remoting_trampoline:
6125 * @method: pointer to the method info
6127 * Creates a trampoline which calls the remoting functions. This
6128 * is used in the vtable of transparent proxies.
6130 * Returns: a pointer to the newly created code
6133 mono_jit_create_remoting_trampoline (MonoMethod *method)
6136 guint8 *addr = NULL;
6138 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
6139 method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class)) {
6140 nm = mono_marshal_get_remoting_invoke (method);
6141 addr = mono_compile_method (nm);
6143 addr = mono_compile_method (method);
6148 static CRITICAL_SECTION ms;
6151 mini_init (const char *filename)
6155 metadata_section = &ms;
6156 InitializeCriticalSection (metadata_section);
6158 mono_jit_tls_id = TlsAlloc ();
6159 mono_thread_start_cb (GetCurrentThreadId (), (gpointer)-1, NULL);
6163 mono_runtime_install_handlers ();
6165 mono_install_compile_method (mono_jit_compile_method);
6166 mono_install_trampoline (mono_arch_create_jit_trampoline);
6167 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
6168 mono_install_runtime_invoke (mono_jit_runtime_invoke);
6169 mono_install_handler (mono_arch_get_throw_exception ());
6170 mono_install_stack_walk (mono_jit_walk_stack);
6171 mono_install_get_config_dir ();
6173 domain = mono_init (filename);
6176 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
6177 ves_icall_get_frame_info);
6178 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
6179 ves_icall_get_trace);
6180 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
6181 mono_runtime_install_handlers);
6184 create_helper_signature ();
6186 mono_arch_register_lowlevel_calls ();
6187 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
6188 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
6190 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
6191 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
6193 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
6194 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
6195 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
6197 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
6198 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
6199 helper_sig_void_ptr, TRUE);
6202 * NOTE, NOTE, NOTE, NOTE:
6203 * when adding emulation for some opcodes, remember to also add a dummy
6204 * rule to the burg files, because we need the arity information to be correct.
6206 mono_register_opcode_emulation (OP_LMUL, helper_sig_long_long_long, mono_llmult);
6207 mono_register_opcode_emulation (OP_LMUL_OVF_UN, helper_sig_long_long_long, mono_llmult_ovf_un);
6208 mono_register_opcode_emulation (OP_LMUL_OVF, helper_sig_long_long_long, mono_llmult_ovf);
6209 mono_register_opcode_emulation (OP_LDIV, helper_sig_long_long_long, mono_lldiv);
6210 mono_register_opcode_emulation (OP_LDIV_UN, helper_sig_long_long_long, mono_lldiv_un);
6211 mono_register_opcode_emulation (OP_LREM, helper_sig_long_long_long, mono_llrem);
6212 mono_register_opcode_emulation (OP_LREM_UN, helper_sig_long_long_long, mono_llrem_un);
6214 mono_register_opcode_emulation (OP_LSHL, helper_sig_long_long_int, mono_lshl);
6215 mono_register_opcode_emulation (OP_LSHR, helper_sig_long_long_int, mono_lshr);
6216 mono_register_opcode_emulation (OP_LSHR_UN, helper_sig_long_long_int, mono_lshr_un);
6218 mono_register_opcode_emulation (OP_FCONV_TO_U8, helper_sig_ulong_double, mono_fconv_u8);
6219 mono_register_opcode_emulation (OP_FCONV_TO_U4, helper_sig_uint_double, mono_fconv_u4);
6220 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, helper_sig_long_double, mono_fconv_ovf_i8);
6221 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, helper_sig_ulong_double, mono_fconv_ovf_u8);
6223 #if SIZEOF_VOID_P == 4
6224 mono_register_opcode_emulation (OP_FCONV_TO_U, helper_sig_uint_double, mono_fconv_u4);
6226 #warning "fixme: add opcode emulation"
6229 /* other jit icalls */
6230 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
6231 helper_sig_ptr_ptr_ptr, FALSE);
6232 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
6233 mono_register_jit_icall (mono_threads_get_static_data, "mono_threads_get_static_data", helper_sig_ptr_int, FALSE);
6234 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
6235 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
6236 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
6237 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
6238 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
6239 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
6240 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
6241 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
6242 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
6243 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
6244 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
6245 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
6246 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
6247 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
6248 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
6249 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
6250 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
6251 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
6252 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
6253 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
6254 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
6255 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
6256 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
6257 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
6258 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
6259 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
6260 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
6262 mono_runtime_init (domain, mono_thread_start_cb,
6263 mono_thread_attach_cb);
6265 //mono_thread_attach (domain);
6269 MonoJitStats mono_jit_stats = {0};
6272 print_jit_stats (void)
6274 if (mono_jit_stats.enabled) {
6275 g_print ("Mono Jit statistics\n");
6276 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
6277 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
6278 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
6279 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
6280 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
6281 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
6282 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
6283 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
6284 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
6285 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
6286 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
6287 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
6288 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
6289 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
6290 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
6291 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
6292 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
6293 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
6295 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
6296 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
6297 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
6298 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
6299 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
6304 mini_cleanup (MonoDomain *domain)
6307 * mono_runtime_cleanup() and mono_domain_finalize () need to
6308 * be called early since they need the execution engine still
6309 * fully working (mono_domain_finalize may invoke managed finalizers
6310 * and mono_runtime_cleanup will wait for other threads to finish).
6312 mono_domain_finalize (domain);
6314 mono_runtime_cleanup (domain);
6316 mono_profiler_shutdown ();
6318 mono_debug_cleanup ();
6319 #ifdef PLATFORM_WIN32
6320 win32_seh_cleanup();
6323 mono_domain_unload (domain, TRUE);
6326 DeleteCriticalSection (metadata_section);
6330 mono_set_defaults (int verbose_level, guint32 opts)
6332 mini_verbose = verbose_level;