X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mono%2Fmini%2Fmini.c;h=fec74a8337a9c1b90b13616b8e8f52e4b103baf3;hb=4bcddd011f31598bcccdf52566a279840ef71b08;hp=1f31b77816c278c3c1b0b6c746ab27bfbd861b31;hpb=655d235a8ee83ac0177d6c20b4ba29aade8a9234;p=mono.git diff --git a/mono/mini/mini.c b/mono/mini/mini.c index 1f31b77816c..fec74a8337a 100644 --- a/mono/mini/mini.c +++ b/mono/mini/mini.c @@ -43,6 +43,8 @@ #include "jit-icalls.c" +#define MONO_IS_COND_BRANCH(op) ((op >= CEE_BEQ && op <= CEE_BLT_UN) || (op >= OP_LBEQ && op <= OP_LBLT_UN) || (op >= OP_FBEQ && op <= OP_FBLT_UN)) + #define MONO_CHECK_THIS(ins) (cfg->method->signature->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0) gboolean mono_arch_handle_exception (struct sigcontext *ctx, gpointer obj, gboolean test_only); @@ -91,11 +93,6 @@ guint32 mono_jit_tls_id = 0; gboolean mono_jit_trace_calls = FALSE; gboolean mono_break_on_exc = FALSE; gboolean mono_compile_aot = FALSE; -gboolean mono_trace_coverage = FALSE; -gboolean mono_jit_profile = FALSE; -MonoDebugFormat mono_debug_format = MONO_DEBUG_FORMAT_NONE; - -CRITICAL_SECTION *metadata_section = NULL; static int mini_verbose = 0; @@ -163,19 +160,28 @@ mono_method_blittable (MonoMethod *method) #endif /* debug function */ -static void +G_GNUC_UNUSED static void print_method_from_ip (void *ip) { MonoJitInfo *ji; char *method; + char *source; + MonoDomain *domain = mono_domain_get (); - ji = mono_jit_info_table_find (mono_domain_get (), ip); + ji = mono_jit_info_table_find (domain, ip); if (!ji) { g_print ("No method at %p\n", ip); return; } method = mono_method_full_name (ji->method, TRUE); - 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); + source = mono_debug_source_location_from_address (ji->method, (int) ip, NULL, domain); + + g_print ("IP %p at offset 0x%x of method %s (%p %p)\n", ip, (char*)ip - (char*)ji->code_start, method, ji->code_start, (char*)ji->code_start + ji->code_size); + + if (source) + g_print ("%s\n", source); + + g_free (source); g_free (method); } @@ -492,34 +498,6 @@ print_method_from_ip (void *ip) (dest)->inst_right = (el2); \ } while (0) -static GHashTable *coverage_hash = NULL; - -MonoCoverageInfo * -mono_allocate_coverage_info (MonoMethod *method, int size) -{ - MonoCoverageInfo *res; - - if (!coverage_hash) - coverage_hash = g_hash_table_new (NULL, NULL); - - res = g_malloc0 (sizeof (MonoCoverageInfo) + sizeof (int) * size * 2); - - res->entries = size; - - g_hash_table_insert (coverage_hash, method, res); - - return res; -} - -MonoCoverageInfo * -mono_get_coverage_info (MonoMethod *method) -{ - if (!coverage_hash) - return NULL; - - return g_hash_table_lookup (coverage_hash, method); -} - #if 0 static gint compare_bblock (gconstpointer a, gconstpointer b) @@ -609,10 +587,13 @@ mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths) clause = &header->clauses [i]; if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->token_or_filter) && (offset < (clause->token_or_filter + filter_lengths [i]))) - return (i << 8) | 128 | clause->flags; + return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags; if (MONO_OFFSET_IN_HANDLER (clause, offset)) { - return (i << 8) | 64 | clause->flags; + if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY) + return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags; + else + return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags; } } @@ -620,7 +601,7 @@ mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths) for (i = 0; i < header->num_clauses; ++i) { clause = &header->clauses [i]; if (MONO_OFFSET_IN_CLAUSE (clause, offset)) - return (i << 8) | clause->flags; + return ((i + 1) << 8) | clause->flags; } return -1; @@ -795,6 +776,12 @@ handle_enum: goto handle_enum; } return CEE_LDOBJ; + case MONO_TYPE_TYPEDBYREF: + return CEE_LDOBJ; + case MONO_TYPE_GENERICINST: + if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE) + return CEE_LDOBJ; + return CEE_LDIND_REF; default: g_error ("unknown type 0x%02x in type_to_ldind", type->type); } @@ -846,9 +833,14 @@ handle_enum: goto handle_enum; } return CEE_STOBJ; - /* fail right now */ + case MONO_TYPE_TYPEDBYREF: + return CEE_STOBJ; + case MONO_TYPE_GENERICINST: + if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE) + return CEE_STOBJ; + return CEE_STIND_REF; default: - g_error ("unknown type %02x in type_to_stind", type->type); + g_error ("unknown type 0x%02x in type_to_stind", type->type); } return -1; } @@ -908,6 +900,18 @@ handle_enum: inst->type = STACK_VTYPE; return; } + case MONO_TYPE_TYPEDBYREF: + inst->klass = mono_defaults.typed_reference_class; + inst->type = STACK_VTYPE; + return; + case MONO_TYPE_GENERICINST: + if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE) { + inst->klass = mono_class_from_mono_type (type); + inst->type = STACK_VTYPE; + } else { + inst->type = STACK_OBJ; + } + return; default: g_error ("unknown type 0x%02x in eval stack type", type->type); } @@ -1297,6 +1301,7 @@ mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode) cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count); } + /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/ mono_jit_stats.allocate_var++; MONO_INST_NEW (cfg, inst, opcode); @@ -1578,8 +1583,10 @@ handle_enum: goto handle_enum; } else return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL; + case MONO_TYPE_TYPEDBYREF: + return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL; default: - g_error ("unknown type %02x in ret_type_to_call_opcode", type->type); + g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type); } return -1; } @@ -1630,6 +1637,108 @@ handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, } } +/* + * Prepare arguments for passing to a function call. + * Return a non-zero value if the arguments can't be passed to the given + * signature. + * The type checks are not yet complete and some conversions may need + * casts on 32 or 64 bit architectures. + */ +static int +check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args) +{ + int i, simple_type; + + if (sig->hasthis) { + if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR) + return 1; + args++; + } + for (i = 0; i < sig->param_count; ++i) { + if (sig->params [i]->byref) { + /* + * check the result of ldelema is only passed as an argument if the byref + * type matches exactly the array element type. + * FIXME: if the argument as been saved on the stack as part of the + * interface variable code (the value was on the stack at a basic block boundary) + * we need to add the check in that case, too. + */ + if (args [i]->opcode == CEE_LDELEMA) { + MonoInst *check; + MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]); + if (!exact_class->valuetype) { + MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE); + check->cil_code = args [i]->cil_code; + check->klass = exact_class; + check->inst_left = args [i]->inst_left; + check->type = STACK_OBJ; + args [i]->inst_left = check; + } + } + if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR) + return 1; + continue; + } + simple_type = sig->params [i]->type; +handle_enum: + switch (simple_type) { + case MONO_TYPE_VOID: + return 1; + continue; + case MONO_TYPE_I1: + case MONO_TYPE_U1: + case MONO_TYPE_BOOLEAN: + case MONO_TYPE_I2: + case MONO_TYPE_U2: + case MONO_TYPE_CHAR: + case MONO_TYPE_I4: + case MONO_TYPE_U4: + if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR) + return 1; + continue; + case MONO_TYPE_I: + case MONO_TYPE_U: + case MONO_TYPE_PTR: + if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ) + return 1; + continue; + case MONO_TYPE_CLASS: + case MONO_TYPE_STRING: + case MONO_TYPE_OBJECT: + case MONO_TYPE_SZARRAY: + case MONO_TYPE_ARRAY: + if (args [i]->type != STACK_OBJ) + return 1; + continue; + case MONO_TYPE_I8: + case MONO_TYPE_U8: + if (args [i]->type != STACK_I8) + return 1; + continue; + case MONO_TYPE_R4: + case MONO_TYPE_R8: + if (args [i]->type != STACK_R8) + return 1; + continue; + case MONO_TYPE_VALUETYPE: + if (sig->params [i]->data.klass->enumtype) { + simple_type = sig->params [i]->data.klass->enum_basetype->type; + goto handle_enum; + } + if (args [i]->type != STACK_VTYPE) + return 1; + continue; + case MONO_TYPE_TYPEDBYREF: + if (args [i]->type != STACK_VTYPE) + return 1; + continue; + default: + g_error ("unknown type 0x%02x in check_call_signature", simple_type); + } + } + return 0; +} + inline static int mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object, const guint8 *ip, gboolean to_end) @@ -1686,6 +1795,7 @@ mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignatu MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end) { MonoCallInst *call; + MonoInst *arg; int i; MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual)); @@ -1695,15 +1805,16 @@ mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignatu call->signature = sig; call = mono_arch_call_opcode (cfg, bblock, call, virtual); - for (i = 0; i < (sig->param_count + sig->hasthis); ++i) { - if (call->args [i]) { - if (!call->args [i]->cil_code) - call->args [i]->cil_code = ip; - if (to_end) - mono_add_ins_to_end (bblock, call->args [i]); - else - MONO_ADD_INS (bblock, call->args [i]); - } + for (arg = call->out_args; arg;) { + MonoInst *narg = arg->next; + arg->next = NULL; + if (!arg->cil_code) + arg->cil_code = ip; + if (to_end) + mono_add_ins_to_end (bblock, arg); + else + MONO_ADD_INS (bblock, arg); + arg = narg; } return call; } @@ -1743,11 +1854,11 @@ mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *met inline static int mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, - MonoInst **args, const guint8 *ip, MonoInst *this) + MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this) { - MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, method->signature, args, ip, this); + MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this); - return mono_spill_call (cfg, bblock, call, method->signature, method->string_ctor, ip, FALSE); + return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE); } inline static int @@ -1780,6 +1891,7 @@ static void mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info) { MonoInst *ins, *temp = NULL, *store, *load; + MonoInst *last_arg = NULL; int i, nargs; MonoCallInst *call; @@ -1805,23 +1917,21 @@ mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJit nargs = info->sig->param_count + info->sig->hasthis; - for (i = 1; i < nargs; i++) { - call->args [i - 1]->next = call->args [i]; - } + for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ; if (nargs) - call->args [nargs - 1]->next = store; + last_arg->next = store; if (cfg->prev_ins) { store->next = cfg->prev_ins->next; if (nargs) - cfg->prev_ins->next = call->args [0]; + cfg->prev_ins->next = call->out_args; else cfg->prev_ins->next = store; } else { store->next = cfg->cbb->code; if (nargs) - cfg->cbb->code = call->args [0]; + cfg->cbb->code = call->out_args; else cfg->cbb->code = store; } @@ -1926,6 +2036,13 @@ handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const MONO_ADD_INS (bblock, ins); break; default: + if (n <= sizeof (gpointer) * 5) { + ins->opcode = OP_MEMSET; + ins->inst_imm = 0; + ins->unused = n; + MONO_ADD_INS (bblock, ins); + break; + } handle_loaded_temps (cfg, bblock, stack_start, sp); NEW_ICONST (cfg, ins, n); iargs [0] = dest; @@ -1937,11 +2054,30 @@ handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S)) +static gboolean +needs_cctor_run (MonoClass *klass, MonoMethod *caller) +{ + int i; + MonoMethod *method; + + for (i = 0; i < klass->method.count; ++i) { + method = klass->methods [i]; + if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) && + (strcmp (".cctor", method->name) == 0)) { + if (caller == method) + return FALSE; + return TRUE; + } + } + return FALSE; +} + static gboolean -mono_method_check_inlining (MonoMethod *method) +mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method) { MonoMethodHeader *header = ((MonoMethodNormal *)method)->header; MonoMethodSignature *signature = method->signature; + MonoVTable *vtable; int i; if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) || @@ -1962,6 +2098,27 @@ mono_method_check_inlining (MonoMethod *method) } } + /* + * if we can initialize the class of the method right away, we do, + * otherwise we don't allow inlining if the class needs initialization, + * since it would mean inserting a call to mono_runtime_class_init() + * inside the inlined code + */ + if (!(cfg->opt & MONO_OPT_SHARED)) { + vtable = mono_class_vtable (cfg->domain, method->klass); + if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) + mono_runtime_class_init (vtable); + else if (!vtable->initialized && needs_cctor_run (method->klass, NULL)) + return FALSE; + } else { + /* + * If we're compiling for shared code + * the cctor will need to be run at aot method load time, for example, + * or at the end of the compilation of the inlining method. + */ + if (needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT))) + return FALSE; + } //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE; /* also consider num_locals? */ @@ -2023,6 +2180,18 @@ mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSig op = OP_SQRT; else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) op = OP_ABS; +#if 0 + /* OP_FREM is not IEEE compatible */ + else if (strcmp (cmethod->name, "IEEERemainder") == 0) + op = OP_FREM; +#endif + else + return NULL; + } else if (cmethod->klass == mono_defaults.array_class) { + if (strcmp (cmethod->name, "get_Rank") == 0) + op = OP_ARRAY_RANK; + else if (strcmp (cmethod->name, "get_Length") == 0) + op = CEE_LDLEN; else return NULL; } else { @@ -2179,6 +2348,9 @@ inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified +#define TYPE_PARAM_TO_TYPE(num) (method->klass->generic_inst->data.generic_inst->type_argv [(num)]) +#define TYPE_PARAM_TO_CLASS(num) (mono_class_from_mono_type (TYPE_PARAM_TO_TYPE ((num)))) + /* offset from br.s -> br like opcodes */ #define BIG_BRANCH_OFFSET 13 @@ -2194,7 +2366,7 @@ get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header while (ip < end) { cli_addr = ip - start; - i = mono_opcode_value (&ip); + i = mono_opcode_value ((const guint8 **)&ip); opcode = &mono_opcodes [i]; switch (opcode->argument) { case MonoInlineNone: @@ -2255,6 +2427,19 @@ unverified: return 1; } +static MonoClassField * +get_generic_field_inst (MonoClassField *field, MonoClass *klass, MonoClass **retclass) +{ + int i; + for (i = 0; i < field->parent->field.count; ++i) { + if (field == &field->parent->fields [i]) { + *retclass = klass; + return &klass->fields [i]; + } + } + return NULL; +} + /* * mono_method_to_ir: translates IL into basic blocks containing trees */ @@ -2299,6 +2484,12 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b bbhash = g_hash_table_new (g_direct_hash, NULL); } + if (cfg->verbose_level > 2) + g_print ("method to IR %s\n", mono_method_full_name (method, TRUE)); + + if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE) + cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size); + dont_inline = g_list_prepend (dont_inline, method); if (cfg->method == method) { @@ -2482,6 +2673,26 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b } } + if (cfg->coverage_info) { + MonoInst *store, *one; + guint32 cil_offset = ip - header->code; + cfg->coverage_info->data [cil_offset].cil_code = ip; + + /* TODO: Use an increment here */ + NEW_ICONST (cfg, one, 1); + one->cil_code = ip; + + NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count)); + ins->cil_code = ip; + + MONO_INST_NEW (cfg, store, CEE_STIND_I); + store->cil_code = ip; + store->inst_left = ins; + store->inst_right = one; + + MONO_ADD_INS (bblock, store); + } + if (cfg->verbose_level > 3) g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL)); @@ -2744,7 +2955,11 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b n = fsig->param_count + fsig->hasthis; } else { - cmethod = mono_get_method (image, token, NULL); + if (method->wrapper_type != MONO_WRAPPER_NONE) { + cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token); + } else { + cmethod = mono_get_method (image, token, NULL); + } if (!cmethod->klass->inited) mono_class_init (cmethod->klass); @@ -2761,7 +2976,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b } #endif } else { - fsig = cmethod->signature; + fsig = mono_method_get_signature (cmethod, image, token); } n = fsig->param_count + fsig->hasthis; @@ -2782,6 +2997,9 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b sp -= n; + if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp)) + goto unverified; + if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL)) { int i; for (i = 0; i < n; ++i) { @@ -2818,7 +3036,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b if ((cfg->opt & MONO_OPT_INLINE) && cmethod && (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) && - mono_method_check_inlining (cmethod) && + mono_method_check_inlining (cfg, cmethod) && !g_list_find (dont_inline, cmethod)) { int costs; MonoBasicBlock *ebblock; @@ -2921,7 +3139,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL); *sp++ = ins; } else { - if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, sp, ip, virtual ? sp [0] : NULL)) != -1) { + if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) { NEW_TEMPLOAD (cfg, *sp, temp); sp++; } @@ -3171,17 +3389,17 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b case CEE_CONV_I1: case CEE_CONV_I2: case CEE_CONV_I4: - case CEE_CONV_I8: case CEE_CONV_R4: case CEE_CONV_R8: case CEE_CONV_U4: + case CEE_CONV_I8: case CEE_CONV_U8: case CEE_CONV_OVF_I8: case CEE_CONV_OVF_U8: case CEE_CONV_R_UN: CHECK_STACK (1); ADD_UNOP (*ip); - ip++; + ip++; break; case CEE_CONV_OVF_I4: case CEE_CONV_OVF_I1: @@ -3228,7 +3446,53 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b ip++; break; case CEE_CPOBJ: - g_error ("opcode 0x%02x not handled", *ip); + CHECK_STACK (2); + token = read32 (ip + 1); + if (method->wrapper_type != MONO_WRAPPER_NONE) + klass = mono_method_get_wrapper_data (method, token); + else + klass = mono_class_get (image, token); + + mono_class_init (klass); + if (klass->byval_arg.type == MONO_TYPE_VAR) + klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param); + sp -= 2; + if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) { + MonoInst *store, *load; + MONO_INST_NEW (cfg, load, CEE_LDIND_REF); + load->cil_code = ip; + load->inst_i0 = sp [1]; + load->type = ldind_type [CEE_LDIND_REF]; + load->flags |= ins_flag; + MONO_INST_NEW (cfg, store, CEE_STIND_REF); + store->cil_code = ip; + handle_loaded_temps (cfg, bblock, stack_start, sp); + MONO_ADD_INS (bblock, store); + store->inst_i0 = sp [0]; + store->inst_i1 = load; + store->flags |= ins_flag; + } else { + n = mono_class_value_size (klass, NULL); + if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) { + MonoInst *copy; + MONO_INST_NEW (cfg, copy, OP_MEMCPY); + copy->inst_left = sp [0]; + copy->inst_right = sp [1]; + copy->cil_code = ip; + copy->unused = n; + MONO_ADD_INS (bblock, copy); + } else { + MonoInst *iargs [3]; + iargs [0] = sp [0]; + iargs [1] = sp [1]; + NEW_ICONST (cfg, iargs [2], n); + iargs [2]->cil_code = ip; + + mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip); + } + } + ins_flag = 0; + ip += 5; break; case CEE_LDOBJ: { MonoInst *iargs [3]; @@ -3241,6 +3505,19 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b klass = mono_class_get (image, token); mono_class_init (klass); + if (klass->byval_arg.type == MONO_TYPE_VAR) + klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param); + if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) { + MONO_INST_NEW (cfg, ins, CEE_LDIND_REF); + ins->cil_code = ip; + ins->inst_i0 = sp [0]; + ins->type = ldind_type [CEE_LDIND_REF]; + ins->flags |= ins_flag; + ins_flag = 0; + *sp++ = ins; + ip += 5; + break; + } n = mono_class_value_size (klass, NULL); ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL); NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0); @@ -3262,6 +3539,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b NEW_TEMPLOAD (cfg, *sp, ins->inst_c0); ++sp; ip += 5; + ins_flag = 0; inline_costs += 1; break; } @@ -3269,31 +3547,44 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b CHECK_STACK_OVF (1); n = read32 (ip + 1); - if (mono_compile_aot) { - cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n); - } - - if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) { + if (method->wrapper_type != MONO_WRAPPER_NONE) { int temp; - MonoInst *iargs [3]; - NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0); - NEW_IMAGECONST (cfg, iargs [1], image); - NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n)); - temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip); + MonoInst *iargs [1]; + + NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n)); + temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip); NEW_TEMPLOAD (cfg, *sp, temp); - mono_ldstr (cfg->domain, image, mono_metadata_token_index (n)); + } else { - NEW_PCONST (cfg, ins, NULL); - ins->cil_code = ip; - ins->type = STACK_OBJ; - ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n)); - *sp = ins; + + if (mono_compile_aot) { + cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n); + } + + if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) { + int temp; + MonoInst *iargs [3]; + NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0); + NEW_IMAGECONST (cfg, iargs [1], image); + NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n)); + temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip); + NEW_TEMPLOAD (cfg, *sp, temp); + mono_ldstr (cfg->domain, image, mono_metadata_token_index (n)); + } else { + NEW_PCONST (cfg, ins, NULL); + ins->cil_code = ip; + ins->type = STACK_OBJ; + ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n)); + *sp = ins; + } } + sp++; ip += 5; break; case CEE_NEWOBJ: { MonoInst *iargs [2]; + MonoMethodSignature *fsig; int temp; token = read32 (ip + 1); @@ -3301,10 +3592,11 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b cmethod = mono_method_get_wrapper_data (method, token); } else cmethod = mono_get_method (image, token, NULL); + fsig = mono_method_get_signature (cmethod, image, token); mono_class_init (cmethod->klass); - n = cmethod->signature->param_count; + n = fsig->param_count; CHECK_STACK (n); /* move the args to allow room for 'this' in the first position */ @@ -3318,13 +3610,13 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b if (cmethod->klass->parent == mono_defaults.array_class) { NEW_METHODCONST (cfg, *sp, cmethod); - temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, cmethod->signature, sp, ip, FALSE); + temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE); } else if (cmethod->string_ctor) { /* we simply pass a null pointer */ NEW_PCONST (cfg, *sp, NULL); /* now call the string ctor */ - temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, sp, ip, NULL); + temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL); } else { if (cmethod->klass->valuetype) { iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL); @@ -3339,18 +3631,21 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b } else { MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass); NEW_PCONST (cfg, iargs [0], vtable); - temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip); + if (cmethod->klass->has_finalize || cmethod->klass->marshalbyref || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS)) + temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip); + else + temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip); } NEW_TEMPLOAD (cfg, *sp, temp); } if ((cfg->opt & MONO_OPT_INLINE) && cmethod && - mono_method_check_inlining (cmethod) && + mono_method_check_inlining (cfg, cmethod) && !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) && !g_list_find (dont_inline, cmethod)) { int costs; MonoBasicBlock *ebblock; - if ((costs = inline_method (cfg, cmethod, cmethod->signature, bblock, sp, ip, real_offset, dont_inline, &ebblock))) { + if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) { ip += 5; real_offset += 5; @@ -3370,11 +3665,11 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b break; } else { - mono_emit_method_call_spilled (cfg, bblock, cmethod, sp, ip, sp[0]); + mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]); } } else { /* now call the actual ctor */ - mono_emit_method_call_spilled (cfg, bblock, cmethod, sp, ip, sp[0]); + mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]); } } @@ -3398,6 +3693,73 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b ip += 5; *sp++ = ins; break; + case CEE_UNBOX_ANY: { + MonoInst *add, *vtoffset; + MonoInst *iargs [3]; + + CHECK_STACK (1); + --sp; + token = read32 (ip + 1); + if (method->wrapper_type != MONO_WRAPPER_NONE) + klass = (MonoClass *)mono_method_get_wrapper_data (method, token); + else + klass = mono_class_get (image, token); + mono_class_init (klass); + + if (klass->byval_arg.type == MONO_TYPE_VAR) + klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param); + + if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) { + /* CASTCLASS */ + MONO_INST_NEW (cfg, ins, CEE_CASTCLASS); + ins->type = STACK_OBJ; + ins->inst_left = *sp; + ins->klass = klass; + ins->inst_newa_class = klass; + ins->cil_code = ip; + *sp++ = ins; + ip += 5; + break; + } + + MONO_INST_NEW (cfg, ins, OP_UNBOXCAST); + ins->type = STACK_OBJ; + ins->inst_left = *sp; + ins->klass = klass; + ins->inst_newa_class = klass; + ins->cil_code = ip; + + MONO_INST_NEW (cfg, add, CEE_ADD); + NEW_ICONST (cfg, vtoffset, sizeof (MonoObject)); + add->inst_left = ins; + add->inst_right = vtoffset; + add->type = STACK_MP; + *sp = add; + ip += 5; + /* LDOBJ impl */ + n = mono_class_value_size (klass, NULL); + ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL); + NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0); + if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) { + MonoInst *copy; + MONO_INST_NEW (cfg, copy, OP_MEMCPY); + copy->inst_left = iargs [0]; + copy->inst_right = *sp; + copy->cil_code = ip; + copy->unused = n; + MONO_ADD_INS (bblock, copy); + } else { + iargs [1] = *sp; + NEW_ICONST (cfg, iargs [2], n); + iargs [2]->cil_code = ip; + + mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip); + } + NEW_TEMPLOAD (cfg, *sp, ins->inst_c0); + ++sp; + inline_costs += 2; + break; + } case CEE_UNBOX: { MonoInst *add, *vtoffset; @@ -3410,6 +3772,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b klass = mono_class_get (image, token); mono_class_init (klass); + if (klass->byval_arg.type == MONO_TYPE_VAR) + klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param); MONO_INST_NEW (cfg, ins, OP_UNBOXCAST); ins->type = STACK_OBJ; @@ -3434,6 +3798,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b --sp; klass = mono_class_get (image, read32 (ip + 1)); mono_class_init (klass); + if (klass->byval_arg.type == MONO_TYPE_VAR) + klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param); ins->type = STACK_OBJ; ins->inst_left = *sp; ins->klass = klass; @@ -3473,6 +3839,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b // goto unverified; token = read32 (ip + 1); field = mono_field_from_token (image, token, &klass); + if (field->parent->gen_params) + field = get_generic_field_inst (field, method->klass, &klass); mono_class_init (klass); foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset; @@ -3508,7 +3876,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b inline_costs += costs; break; } else { - mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, iargs, ip, NULL); + mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL); } } else { MonoInst *store; @@ -3572,7 +3940,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b inline_costs += costs; break; } else { - temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, iargs, ip, NULL); + temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL); if (*ip == CEE_LDFLDA) { /* not sure howto handle this */ NEW_TEMPLOADA (cfg, *sp, temp); @@ -3610,7 +3978,6 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b case CEE_LDSFLDA: case CEE_STSFLD: { MonoClassField *field; - MonoVTable *vtable; token = read32 (ip + 1); @@ -3629,8 +3996,18 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b NEW_TEMPLOAD (cfg, ins, temp); } else { gpointer addr; + MonoVTable *vtable; vtable = mono_class_vtable (cfg->domain, klass); if (!cfg->domain->thread_static_fields || !(addr = g_hash_table_lookup (cfg->domain->thread_static_fields, field))) { + if (!vtable->initialized && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && needs_cctor_run (klass, method)) { + MonoInst *iargs [1]; + NEW_PCONST (cfg, iargs [0], vtable); + mono_emit_jit_icall (cfg, bblock, mono_runtime_class_init, iargs, ip); + if (cfg->verbose_level > 2) + g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name); + } else { + mono_runtime_class_init (vtable); + } addr = (char*)vtable->data + field->offset; NEW_PCONST (cfg, ins, addr); ins->cil_code = ip; @@ -3667,36 +4044,117 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b } else MONO_ADD_INS (bblock, store); } else { - MonoInst *load; - CHECK_STACK_OVF (1); - MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type)); - type_to_eval_stack_type (field->type, load); - load->cil_code = ip; - load->inst_left = ins; - *sp++ = load; - load->flags |= ins_flag; - ins_flag = 0; - /* fixme: dont see the problem why this does not work */ - //cfg->disable_aot = TRUE; - } - ip += 5; - break; - } - case CEE_STOBJ: - CHECK_STACK (2); - sp -= 2; - token = read32 (ip + 1); - if (method->wrapper_type != MONO_WRAPPER_NONE) - klass = mono_method_get_wrapper_data (method, token); - else - klass = mono_class_get (image, token); - mono_class_init (klass); - handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE); - ip += 5; - inline_costs += 1; - break; - case CEE_BOX: { - MonoInst *iargs [2]; + gboolean is_const = FALSE; + MonoVTable *vtable = mono_class_vtable (cfg->domain, klass); + if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) && + vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) { + gpointer addr = (char*)vtable->data + field->offset; + /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/ + is_const = TRUE; + switch (field->type->type) { + case MONO_TYPE_BOOLEAN: + case MONO_TYPE_U1: + NEW_ICONST (cfg, *sp, *((guint8 *)addr)); + sp++; + break; + case MONO_TYPE_I1: + NEW_ICONST (cfg, *sp, *((gint8 *)addr)); + sp++; + break; + case MONO_TYPE_CHAR: + case MONO_TYPE_U2: + NEW_ICONST (cfg, *sp, *((guint16 *)addr)); + sp++; + break; + case MONO_TYPE_I2: + NEW_ICONST (cfg, *sp, *((gint16 *)addr)); + sp++; + break; + break; + case MONO_TYPE_I4: + NEW_ICONST (cfg, *sp, *((gint32 *)addr)); + sp++; + break; + case MONO_TYPE_U4: + NEW_ICONST (cfg, *sp, *((guint32 *)addr)); + sp++; + break; + case MONO_TYPE_I: + case MONO_TYPE_U: + case MONO_TYPE_STRING: + case MONO_TYPE_OBJECT: + case MONO_TYPE_CLASS: + case MONO_TYPE_SZARRAY: + case MONO_TYPE_PTR: + case MONO_TYPE_FNPTR: + case MONO_TYPE_ARRAY: + NEW_PCONST (cfg, *sp, *((gpointer *)addr)); + type_to_eval_stack_type (field->type, *sp); + sp++; + break; + case MONO_TYPE_I8: + case MONO_TYPE_U8: + MONO_INST_NEW (cfg, *sp, OP_I8CONST); + sp [0]->type = STACK_I8; + sp [0]->inst_l = *((gint64 *)addr); + sp++; + break; + case MONO_TYPE_R4: + case MONO_TYPE_R8: + case MONO_TYPE_VALUETYPE: + default: + is_const = FALSE; + break; + } + } + + if (!is_const) { + MonoInst *load; + CHECK_STACK_OVF (1); + MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type)); + type_to_eval_stack_type (field->type, load); + load->cil_code = ip; + load->inst_left = ins; + *sp++ = load; + load->flags |= ins_flag; + ins_flag = 0; + /* fixme: dont see the problem why this does not work */ + //cfg->disable_aot = TRUE; + } + } + ip += 5; + break; + } + case CEE_STOBJ: + CHECK_STACK (2); + sp -= 2; + token = read32 (ip + 1); + if (method->wrapper_type != MONO_WRAPPER_NONE) + klass = mono_method_get_wrapper_data (method, token); + else + klass = mono_class_get (image, token); + mono_class_init (klass); + if (klass->byval_arg.type == MONO_TYPE_VAR) + klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param); + n = mono_type_to_stind (&klass->byval_arg); + if (n == CEE_STOBJ) { + handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE); + } else { + /* FIXME: should check item at sp [1] is compatible with the type of the store. */ + MonoInst *store; + MONO_INST_NEW (cfg, store, n); + store->cil_code = ip; + store->inst_left = sp [0]; + store->inst_right = sp [1]; + store->flags |= ins_flag; + MONO_ADD_INS (bblock, store); + } + ins_flag = 0; + ip += 5; + inline_costs += 1; + break; + case CEE_BOX: { + MonoInst *iargs [2]; MonoInst *load, *vtoffset, *add, *val, *vstore; int temp; CHECK_STACK (1); @@ -3708,12 +4166,28 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b else klass = mono_class_get (image, token); mono_class_init (klass); + if (klass->byval_arg.type == MONO_TYPE_VAR) + klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param); + if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) { + *sp = val; + ip += 5; + break; + } /* much like NEWOBJ */ - NEW_DOMAINCONST (cfg, iargs [0]); - NEW_CLASSCONST (cfg, iargs [1], klass); - - temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip); + if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) { + NEW_DOMAINCONST (cfg, iargs [0]); + NEW_CLASSCONST (cfg, iargs [1], klass); + + temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip); + } else { + MonoVTable *vtable = mono_class_vtable (cfg->domain, klass); + NEW_PCONST (cfg, iargs [0], vtable); + if (1 || klass->has_finalize || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS)) + temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip); + else + temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip); + } NEW_TEMPLOAD (cfg, load, temp); NEW_ICONST (cfg, vtoffset, sizeof (MonoObject)); MONO_INST_NEW (cfg, add, CEE_ADD); @@ -3756,6 +4230,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b klass = mono_class_get (image, token); mono_class_init (klass); + if (klass->byval_arg.type == MONO_TYPE_VAR) + klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param); ins->inst_newa_class = klass; ins->inst_newa_len = *sp; ins->type = STACK_OBJ; @@ -3777,11 +4253,32 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b sp -= 2; klass = mono_class_get (image, read32 (ip + 1)); mono_class_init (klass); + if (klass->byval_arg.type == MONO_TYPE_VAR) + klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param); NEW_LDELEMA (cfg, ins, sp, klass); ins->cil_code = ip; *sp++ = ins; ip += 5; break; + case CEE_LDELEM: { + MonoInst *load; + CHECK_STACK (2); + sp -= 2; + token = read32 (ip + 1); + klass = mono_class_get (image, token); + mono_class_init (klass); + if (klass->byval_arg.type == MONO_TYPE_VAR) + klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param); + NEW_LDELEMA (cfg, load, sp, klass); + load->cil_code = ip; + MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg)); + ins->cil_code = ip; + ins->inst_left = load; + *sp++ = ins; + type_to_eval_stack_type (&klass->byval_arg, ins); + ip += 5; + break; + } case CEE_LDELEM_I1: case CEE_LDELEM_U1: case CEE_LDELEM_I2: @@ -3837,7 +4334,44 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b ++ip; handle_loaded_temps (cfg, bblock, stack_start, sp); MONO_ADD_INS (bblock, ins); - /* FIXME: add the implicit STELEM_REF castclass */ + inline_costs += 1; + cfg->disable_ssa = TRUE; + break; + } + case CEE_STELEM: { + MonoInst *load; + /* + * translate to: + * stind.x (ldelema (array, index), val) + * ldelema does the bounds check + */ + CHECK_STACK (3); + sp -= 3; + token = read32 (ip + 1); + klass = mono_class_get (image, token); + mono_class_init (klass); + if (klass->byval_arg.type == MONO_TYPE_VAR) + klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param); + if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) { + MonoInst *iargs [3]; + handle_loaded_temps (cfg, bblock, stack_start, sp); + + iargs [2] = sp [2]; + iargs [1] = sp [1]; + iargs [0] = sp [0]; + + mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip); + } else { + NEW_LDELEMA (cfg, load, sp, klass); + load->cil_code = ip; + MONO_INST_NEW (cfg, ins, mono_type_to_stind (&klass->byval_arg)); + ins->cil_code = ip; + ins->inst_left = load; + ins->inst_right = sp [2]; + handle_loaded_temps (cfg, bblock, stack_start, sp); + MONO_ADD_INS (bblock, ins); + } + ip += 5; inline_costs += 1; cfg->disable_ssa = TRUE; break; @@ -4231,9 +4765,24 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b } case CEE_PREFIX1: { switch (ip [1]) { - case CEE_ARGLIST: - g_error ("opcode 0xfe 0x%02x not handled", ip [1]); + case CEE_ARGLIST: { + /* somewhat similar to LDTOKEN */ + MonoInst *addr, *vtvar; + CHECK_STACK_OVF (1); + vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL); + + NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0); + addr->cil_code = ip; + MONO_INST_NEW (cfg, ins, OP_ARGLIST); + ins->cil_code = ip; + ins->inst_left = addr; + MONO_ADD_INS (bblock, ins); + NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0); + ins->cil_code = ip; + *sp++ = ins; + ip += 2; break; + } case CEE_CEQ: case CEE_CGT: case CEE_CGT_UN: @@ -4435,10 +4984,31 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b klass = mono_method_get_wrapper_data (method, token); else klass = mono_class_get (image, token); - handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp); + if (klass->byval_arg.type == MONO_TYPE_VAR) + klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param); + if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) { + MonoInst *store, *load; + NEW_PCONST (cfg, load, NULL); + load->cil_code = ip; + load->type = STACK_OBJ; + MONO_INST_NEW (cfg, store, CEE_STIND_REF); + store->cil_code = ip; + handle_loaded_temps (cfg, bblock, stack_start, sp); + MONO_ADD_INS (bblock, store); + store->inst_i0 = sp [0]; + store->inst_i1 = load; + break; + } else { + handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp); + } ip += 6; inline_costs += 1; break; + case CEE_CONSTRAINED_: + /* FIXME: implement */ + token = read32 (ip + 2); + ip += 6; + break; case CEE_CPBLK: case CEE_INITBLK: { MonoInst *iargs [3]; @@ -4457,6 +5027,16 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b inline_costs += 1; break; } + case CEE_NO_: + if (ip [2] & 0x1) + ins_flag |= MONO_INST_NOTYPECHECK; + if (ip [2] & 0x2) + ins_flag |= MONO_INST_NORANGECHECK; + /* we ignore the no-nullcheck for now since we + * really do it explicitly only when doing callvirt->call + */ + ip += 3; + break; case CEE_RETHROW: { MonoInst *load; /* FIXME: check we are in a catch handler */ @@ -4474,6 +5054,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b case CEE_SIZEOF: CHECK_STACK_OVF (1); token = read32 (ip + 2); + /* FIXXME: handle generics. */ if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) { MonoType *type = mono_type_create_from_typespec (image, token); token = mono_type_size (type, &align); @@ -4491,6 +5072,9 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b case CEE_REFANYTYPE: g_error ("opcode 0xfe 0x%02x not handled", ip [1]); break; + case CEE_READONLY_: + ip += 2; + break; default: g_error ("opcode 0xfe 0x%02x not handled", ip [1]); } @@ -4913,12 +5497,25 @@ mono_find_jit_icall_by_addr (gconstpointer addr) return g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr); } +gconstpointer +mono_icall_get_wrapper (MonoJitICallInfo* callinfo) +{ + char *name; + MonoMethod *wrapper; + + if (callinfo->wrapper) + return callinfo->wrapper; + name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name); + wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func); + callinfo->wrapper = mono_jit_compile_method (wrapper); + g_free (name); + return callinfo->wrapper; +} + MonoJitICallInfo * mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save) { MonoJitICallInfo *info; - MonoMethod *wrapper; - char *n; g_assert (func); g_assert (name); @@ -4935,7 +5532,7 @@ mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignatu info = g_new (MonoJitICallInfo, 1); - info->name = g_strdup (name); + info->name = name; info->func = func; info->sig = sig; @@ -4946,14 +5543,11 @@ mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignatu ) { info->wrapper = func; } else { - g_assert (sig); - n = g_strdup_printf ("__icall_wrapper_%s", name); - wrapper = mono_marshal_get_icall_wrapper (sig, n, func); - info->wrapper = mono_jit_compile_method (wrapper); - g_free (n); + info->wrapper = NULL; + mono_icall_get_wrapper (info); } - g_hash_table_insert (jit_icall_hash_name, info->name, info); + g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info); g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info); if (func != info->wrapper) g_hash_table_insert (jit_icall_hash_addr, (gpointer)info->wrapper, info); @@ -4973,10 +5567,9 @@ mono_find_jit_opcode_emulation (int opcode) } void -mono_register_opcode_emulation (int opcode, MonoMethodSignature *sig, gpointer func) +mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw) { MonoJitICallInfo *info; - char *name; if (!emul_opcode_hash) emul_opcode_hash = g_hash_table_new (NULL, NULL); @@ -4984,11 +5577,7 @@ mono_register_opcode_emulation (int opcode, MonoMethodSignature *sig, gpointer f g_assert (!sig->hasthis); g_assert (sig->param_count < 3); - name = g_strdup_printf ("__emulate_%s", mono_inst_name (opcode)); - - info = mono_register_jit_icall (func, name, sig, FALSE); - - g_free (name); + info = mono_register_jit_icall (func, name, sig, no_throw); g_hash_table_insert (emul_opcode_hash, (gpointer)opcode, info); } @@ -5020,8 +5609,7 @@ decompose_foreach (MonoInst *tree, gpointer data) info = newarr_info; } else { - MonoVTable *vtable = mono_class_vtable (cfg->domain, - mono_array_class_get (&tree->inst_newa_class->byval_arg, 1)); + MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1)); NEW_PCONST (cfg, iargs [0], vtable); iargs [1] = tree->inst_newa_len; @@ -5212,28 +5800,40 @@ mono_thread_abort (MonoObject *obj) { MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); - g_free (jit_tls); + /* handle_remove should be eventually called for this thread, too + g_free (jit_tls);*/ ExitThread (-1); } static void -mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func) +setup_jit_tls_data (gpointer stack_start, gpointer abort_func) { MonoJitTlsData *jit_tls; MonoLMF *lmf; + MonoThread *thread; jit_tls = g_new0 (MonoJitTlsData, 1); TlsSetValue (mono_jit_tls_id, jit_tls); - jit_tls->abort_func = mono_thread_abort; + jit_tls->abort_func = abort_func; jit_tls->end_of_stack = stack_start; lmf = g_new0 (MonoLMF, 1); lmf->ebp = -1; - jit_tls->lmf = lmf; + jit_tls->lmf = jit_tls->first_lmf = lmf; + + thread = mono_thread_current (); + if (thread) + thread->jit_data = jit_tls; +} + +static void +mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func) +{ + setup_jit_tls_data (stack_start, mono_thread_abort); } void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL; @@ -5250,20 +5850,19 @@ mono_thread_abort_dummy (MonoObject *obj) static void mono_thread_attach_cb (guint32 tid, gpointer stack_start) { - MonoJitTlsData *jit_tls; - MonoLMF *lmf; - - jit_tls = g_new0 (MonoJitTlsData, 1); - - TlsSetValue (mono_jit_tls_id, jit_tls); - - jit_tls->abort_func = mono_thread_abort_dummy; - jit_tls->end_of_stack = stack_start; + setup_jit_tls_data (stack_start, mono_thread_abort_dummy); +} - lmf = g_new0 (MonoLMF, 1); - lmf->ebp = -1; +static void +mini_thread_cleanup (MonoThread *thread) +{ + MonoJitTlsData *jit_tls = thread->jit_data; - jit_tls->lmf = lmf; + if (jit_tls) { + g_free (jit_tls->first_lmf); + g_free (jit_tls); + thread->jit_data = NULL; + } } void @@ -5311,8 +5910,20 @@ dec_foreach (MonoInst *tree, MonoCompile *cfg) { } break; - case 2: - if ((info = mono_find_jit_opcode_emulation (tree->opcode))) { + case 2: + if (tree->opcode == OP_LMUL + && (cfg->opt & MONO_OPT_INTRINS) + && (tree->inst_left->opcode == CEE_CONV_I8 + || tree->inst_left->opcode == CEE_CONV_U8) + && tree->inst_left->inst_left->type == STACK_I4 + && (tree->inst_right->opcode == CEE_CONV_I8 + || tree->inst_right->opcode == CEE_CONV_U8) + && tree->inst_right->inst_left->type == STACK_I4) { + tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN); + tree->inst_left = tree->inst_left->inst_left; + tree->inst_right = tree->inst_right->inst_left; + dec_foreach (tree, cfg); + } else if ((info = mono_find_jit_opcode_emulation (tree->opcode))) { MonoInst *iargs [2]; iargs [0] = tree->inst_i0; @@ -5358,6 +5969,47 @@ nullify_basic_block (MonoBasicBlock *bb) bb->out_bb = NULL; bb->next_bb = NULL; bb->code = bb->last_ins = NULL; + bb->cil_code = NULL; +} + +static void +replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl) +{ + int i; + + for (i = 0; i < bb->out_count; i++) { + MonoBasicBlock *ob = bb->out_bb [i]; + if (ob == orig) { + if (!repl) { + if (bb->out_count > 1) { + bb->out_bb [i] = bb->out_bb [bb->out_count - 1]; + } + bb->out_count--; + } else { + bb->out_bb [i] = repl; + } + } + } +} + +static void +replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl) +{ + int i; + + for (i = 0; i < bb->in_count; i++) { + MonoBasicBlock *ib = bb->in_bb [i]; + if (ib == orig) { + if (!repl) { + if (bb->in_count > 1) { + bb->in_bb [i] = bb->in_bb [bb->in_count - 1]; + } + bb->in_count--; + } else { + bb->in_bb [i] = repl; + } + } + } } static void @@ -5368,13 +6020,15 @@ replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock * for (i = 0; i < bb->out_count; i++) { MonoBasicBlock *ob = bb->out_bb [i]; for (j = 0; j < ob->in_count; j++) { - if (ob->in_bb [j] == orig) + if (ob->in_bb [j] == orig) { ob->in_bb [j] = repl; + } } } } + static void merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn) { @@ -5398,7 +6052,7 @@ merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn) static void optimize_branches (MonoCompile *cfg) { - int changed = FALSE; + int i, changed = FALSE; MonoBasicBlock *bb, *bbn; do { @@ -5411,26 +6065,42 @@ optimize_branches (MonoCompile *cfg) { if (bb->region != -1) continue; + if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) { + if (cfg->verbose_level > 2) + g_print ("nullify block triggered %d\n", bbn->block_num); + + bb->next_bb = bbn->next_bb; + + for (i = 0; i < bbn->out_count; i++) + replace_in_block (bbn->out_bb [i], bbn, NULL); + + nullify_basic_block (bbn); + changed = TRUE; + } + if (bb->out_count == 1) { bbn = bb->out_bb [0]; + /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */ + if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) { + bb->last_ins->opcode = CEE_BR; + bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb; + changed = TRUE; + if (cfg->verbose_level > 2) + g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count); + } + if (bb->region == bbn->region && bb->next_bb == bbn) { - /* the block are in sequence anyway ... */ + /* the block are in sequence anyway ... */ - /* - * miguel: I do not understand what the test below does, could we - * use a macro, or a comment here? opcode > CEE_BEQ && <= BLT_UN - * - * It could also test for bb->last_in only once, and the value - * could be cached (last_ins->opcode) - */ - if (bb->last_ins && (bb->last_ins->opcode == CEE_BR || ( - (bb->last_ins && bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN)))) { + /* branches to the following block can be removed */ + if (bb->last_ins && bb->last_ins->opcode == CEE_BR) { bb->last_ins->opcode = CEE_NOP; changed = TRUE; if (cfg->verbose_level > 2) g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num); } + if (bbn->in_count == 1) { if (bbn != cfg->bb_exit) { @@ -5442,53 +6112,102 @@ optimize_branches (MonoCompile *cfg) { //mono_print_bb_code (bb); } - } else { - if (bb->last_ins && bb->last_ins->opcode == CEE_BR) { - bbn = bb->last_ins->inst_target_bb; - if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR && - bbn->code->inst_target_bb->region == bb->region) { - - if (cfg->verbose_level > 2) - g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name, - bb->block_num, bbn->block_num); + } + } + } + } while (changed); - replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb); - bb->last_ins->inst_target_bb = bbn->code->inst_target_bb; - changed = TRUE; - } - } + do { + changed = FALSE; + + /* we skip the entry block (exit is handled specially instead ) */ + for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) { + + /* dont touch code inside exception clauses */ + if (bb->region != -1) + continue; + + if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) { + if (cfg->verbose_level > 2) { + g_print ("nullify block triggered %d\n", bbn->block_num); } + bb->next_bb = bbn->next_bb; - } else if (bb->out_count == 2) { - /* fixme: this does not correctly - no idea whats wrong */ - if (0 && bb->last_ins && bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) { - bbn = bb->last_ins->inst_true_bb; + for (i = 0; i < bbn->out_count; i++) + replace_in_block (bbn->out_bb [i], bbn, NULL); + + nullify_basic_block (bbn); + changed = TRUE; + break; + } + + + if (bb->out_count == 1) { + bbn = bb->out_bb [0]; + + if (bb->last_ins && bb->last_ins->opcode == CEE_BR) { + bbn = bb->last_ins->inst_target_bb; if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR && bbn->code->inst_target_bb->region == bb->region) { + if (cfg->verbose_level > 2) - g_print ("cbranch to branch triggered %d -> %d (0x%02x)\n", bb->block_num, - bbn->block_num, bbn->code->opcode); - + g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name, + bb->block_num, bbn->block_num); + replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb); + bb->last_ins->inst_target_bb = bbn->code->inst_target_bb; + changed = TRUE; + break; + } + } + } else if (bb->out_count == 2) { + if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) { + bbn = bb->last_ins->inst_true_bb; + if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR && + bbn->code->inst_target_bb->region == bb->region) { + if (cfg->verbose_level > 2) + g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n", + bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num, + bbn->code->opcode); + bb->last_ins->inst_true_bb = bbn->code->inst_target_bb; + + replace_in_block (bbn, bb, NULL); + if (!bbn->in_count) + replace_in_block (bbn->code->inst_target_bb, bbn, bb); + replace_out_block (bb, bbn, bbn->code->inst_target_bb); + + link_bblock (cfg, bb, bbn->code->inst_target_bb); + changed = TRUE; + break; } bbn = bb->last_ins->inst_false_bb; if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR && bbn->code->inst_target_bb->region == bb->region) { if (cfg->verbose_level > 2) - g_print ("cbranch to branch triggered %d -> %d (0x%02x)\n", bb->block_num, - bbn->block_num, bbn->code->opcode); - - replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb); + g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n", + bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num, + bbn->code->opcode); + bb->last_ins->inst_false_bb = bbn->code->inst_target_bb; + + replace_in_block (bbn, bb, NULL); + if (!bbn->in_count) + replace_in_block (bbn->code->inst_target_bb, bbn, bb); + replace_out_block (bb, bbn, bbn->code->inst_target_bb); + + link_bblock (cfg, bb, bbn->code->inst_target_bb); + changed = TRUE; + break; } } } } } while (changed); + } static void @@ -5508,6 +6227,8 @@ mono_compile_create_vars (MonoCompile *cfg) cfg->ret->inst_vtype = sig->ret; cfg->ret->klass = mono_class_from_mono_type (sig->ret); } + if (cfg->verbose_level > 2) + g_print ("creating vars\n"); if (sig->hasthis) mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG); @@ -5517,8 +6238,12 @@ mono_compile_create_vars (MonoCompile *cfg) cfg->locals_start = cfg->num_varinfo; + if (cfg->verbose_level > 2) + g_print ("creating locals\n"); for (i = 0; i < header->num_locals; ++i) mono_compile_create_var (cfg, header->locals [i], OP_LOCAL); + if (cfg->verbose_level > 2) + g_print ("locals done\n"); } #if 0 @@ -5599,11 +6324,49 @@ emit_state (MonoCompile *cfg, MBState *state, int goal) static void mini_select_instructions (MonoCompile *cfg) { + static int reverse_map [] = { + CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE, + CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN + }; + static int reverse_fmap [] = { + OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE, + OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN + }; + static int reverse_lmap [] = { + OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE, + OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN + }; + MonoBasicBlock *bb; cfg->state_pool = mono_mempool_new (); cfg->rs = mono_regstate_new (); + for (bb = cfg->bb_entry; bb; bb = bb->next_bb) { + if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode) && + bb->next_bb != bb->last_ins->inst_false_bb) { + + if (bb->next_bb == bb->last_ins->inst_true_bb) { + MonoBasicBlock *tmp = bb->last_ins->inst_true_bb; + bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb; + bb->last_ins->inst_false_bb = tmp; + + if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) { + bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ]; + } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) { + bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ]; + } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) { + bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ]; + } + } else { + MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst)); + inst->opcode = CEE_BR; + inst->inst_target_bb = bb->last_ins->inst_false_bb; + mono_bblock_add_inst (bb, inst); + } + } + } + #ifdef DEBUG_SELECTION if (cfg->verbose_level >= 4) { for (bb = cfg->bb_entry; bb; bb = bb->next_bb) { @@ -5678,12 +6441,12 @@ mono_codegen (MonoCompile *cfg) mono_arch_local_regalloc (cfg, bb); } - if (mono_trace_coverage) - mono_allocate_coverage_info (cfg->method, cfg->num_bblocks); + if (cfg->prof_options & MONO_PROFILE_COVERAGE) + cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks); code = mono_arch_emit_prolog (cfg); - if (mono_jit_profile) + if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE) code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE); cfg->code_len = code - cfg->native_code; @@ -5715,7 +6478,7 @@ mono_codegen (MonoCompile *cfg) cfg->epilog_begin = cfg->code_len; - if (mono_jit_profile) + if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE) code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE); cfg->code_len = code - cfg->native_code; @@ -5951,37 +6714,47 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int p int dfn = 0, i, code_size_ratio; mono_jit_stats.methods_compiled++; - if (mono_jit_profile) + if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION) mono_profiler_method_jit (method); cfg = g_new0 (MonoCompile, 1); cfg->method = method; cfg->mempool = mono_mempool_new (); cfg->opt = opts; + cfg->prof_options = mono_profiler_get_events (); cfg->bb_hash = g_hash_table_new (g_direct_hash, NULL); cfg->domain = domain; cfg->verbose_level = mini_verbose; cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX * ((MonoMethodNormal *)method)->header->max_stack); + if (cfg->verbose_level > 2) + g_print ("converting method %s\n", mono_method_full_name (method, TRUE)); + /* * create MonoInst* which represents arguments and local variables */ mono_compile_create_vars (cfg); - if (cfg->verbose_level > 2) - g_print ("converting method %s\n", mono_method_full_name (method, TRUE)); - if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) { - mono_destroy_compile (cfg); - if (mono_jit_profile) + if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION) mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED); + mono_destroy_compile (cfg); return NULL; } mono_jit_stats.basic_blocks += cfg->num_bblocks; mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks); + if (cfg->num_varinfo > 2000) { + /* + * we disable some optimizations if there are too many variables + * because JIT time may become too expensive. The actual number needs + * to be tweaked and eventually the non-linear algorithms should be fixed. + */ + cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP); + cfg->disable_ssa = TRUE; + } /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/ /* Depth-first ordering on basic blocks */ @@ -5992,9 +6765,26 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int p df_visit (cfg->bb_entry, &dfn, cfg->bblocks); if (cfg->num_bblocks != dfn + 1) { - if (cfg->verbose_level > 1) - g_print ("unreachable code?\n"); + MonoBasicBlock *bb; + cfg->num_bblocks = dfn + 1; + + if (!header->clauses) { + /* remove unreachable code, because the code in them may be + * inconsistent (access to dead variables for example) */ + for (bb = cfg->bb_entry; bb;) { + MonoBasicBlock *bbn = bb->next_bb; + + if (bbn && bbn->region == -1 && !bbn->dfn) { + if (cfg->verbose_level > 1) + g_print ("found unreachabel code in BB%d\n", bbn->block_num); + bb->next_bb = bbn->next_bb; + nullify_basic_block (bbn); + } else { + bb = bb->next_bb; + } + } + } } if (cfg->opt & MONO_OPT_LOOP) { @@ -6076,9 +6866,9 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int p } //mono_print_code (cfg); - - //print_dfn (cfg); + //print_dfn (cfg); + /* variables are allocated after decompose, since decompose could create temps */ mono_arch_allocate_vars (cfg); @@ -6155,7 +6945,7 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int p } mono_jit_stats.native_code_size += cfg->code_len; - if (mono_jit_profile) + if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION) mono_profiler_method_end_jit (method, MONO_PROFILE_OK); return cfg; @@ -6258,7 +7048,7 @@ mono_jit_compile_method (MonoMethod *method) g_slist_free (list); } /* make sure runtime_init is called */ - mono_class_vtable (target_domain, method->klass); + mono_runtime_class_init (mono_class_vtable (target_domain, method->klass)); return code; } @@ -6334,6 +7124,18 @@ sigusr1_signal_handler (int _dummy) mono_arch_handle_exception (ctx, thread->abort_exc, FALSE); } +static void +sigquit_signal_handler (int _dummy) +{ + MonoException *exc; + GET_CONTEXT + + exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT)."); + + mono_arch_handle_exception (ctx, exc, FALSE); +} + + static void mono_runtime_install_handlers (void) { @@ -6360,6 +7162,12 @@ mono_runtime_install_handlers (void) //g_assert (syscall (SYS_sigaction, SIGFPE, &sa, NULL) != -1); g_assert (sigaction (SIGFPE, &sa, NULL) != -1); + /* catch SIGQUIT */ + sa.sa_handler = sigquit_signal_handler; + sigemptyset (&sa.sa_mask); + sa.sa_flags = 0; + g_assert (sigaction (SIGQUIT, &sa, NULL) != -1); + /* catch SIGILL */ sa.sa_handler = sigill_signal_handler; sigemptyset (&sa.sa_mask); @@ -6415,7 +7223,9 @@ MonoDomain * mini_init (const char *filename) { MonoDomain *domain; - + + mono_arch_cpu_init (); + metadata_section = &ms; InitializeCriticalSection (metadata_section); @@ -6425,6 +7235,7 @@ mini_init (const char *filename) mono_burg_init (); mono_runtime_install_handlers (); + mono_threads_install_cleanup (mini_thread_cleanup); mono_install_compile_method (mono_jit_compile_method); mono_install_trampoline (mono_arch_create_jit_trampoline); @@ -6467,25 +7278,25 @@ mini_init (const char *filename) * when adding emulation for some opcodes, remember to also add a dummy * rule to the burg files, because we need the arity information to be correct. */ - mono_register_opcode_emulation (OP_LMUL, helper_sig_long_long_long, mono_llmult); - mono_register_opcode_emulation (OP_LMUL_OVF_UN, helper_sig_long_long_long, mono_llmult_ovf_un); - mono_register_opcode_emulation (OP_LMUL_OVF, helper_sig_long_long_long, mono_llmult_ovf); - mono_register_opcode_emulation (OP_LDIV, helper_sig_long_long_long, mono_lldiv); - mono_register_opcode_emulation (OP_LDIV_UN, helper_sig_long_long_long, mono_lldiv_un); - mono_register_opcode_emulation (OP_LREM, helper_sig_long_long_long, mono_llrem); - mono_register_opcode_emulation (OP_LREM_UN, helper_sig_long_long_long, mono_llrem_un); - - mono_register_opcode_emulation (OP_LSHL, helper_sig_long_long_int, mono_lshl); - mono_register_opcode_emulation (OP_LSHR, helper_sig_long_long_int, mono_lshr); - mono_register_opcode_emulation (OP_LSHR_UN, helper_sig_long_long_int, mono_lshr_un); - - mono_register_opcode_emulation (OP_FCONV_TO_U8, helper_sig_ulong_double, mono_fconv_u8); - mono_register_opcode_emulation (OP_FCONV_TO_U4, helper_sig_uint_double, mono_fconv_u4); - mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, helper_sig_long_double, mono_fconv_ovf_i8); - mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, helper_sig_ulong_double, mono_fconv_ovf_u8); + mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE); + mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE); + mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE); + mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE); + mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE); + mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE); + mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE); + + mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE); + mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE); + mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE); + + mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE); + mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE); + mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, TRUE); + mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, TRUE); #if SIZEOF_VOID_P == 4 - mono_register_opcode_emulation (OP_FCONV_TO_U, helper_sig_uint_double, mono_fconv_u4); + mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE); #else #warning "fixme: add opcode emulation" #endif @@ -6502,6 +7313,7 @@ mini_init (const char *filename) mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE); mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE); mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE); + mono_register_jit_icall (mono_object_new_fast, "mono_object_new_fast", helper_sig_object_new_specific, FALSE); mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE); mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE); mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE); @@ -6520,11 +7332,12 @@ mini_init (const char *filename) mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE); mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE); mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE); + mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE); mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE); mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE); - mono_runtime_init (domain, mono_thread_start_cb, - mono_thread_attach_cb); + mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup); + mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb); //mono_thread_attach (domain); return domain; @@ -6580,6 +7393,7 @@ mini_cleanup (MonoDomain *domain) mono_profiler_shutdown (); mono_debug_cleanup (); + #ifdef PLATFORM_WIN32 win32_seh_cleanup(); #endif