2006-07-19 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / mini.c
index 47568c1d543658904012cf8aeb00049e8a81ba33..10f42e6e2bbca395e8a9b2de8e1e629813256313 100644 (file)
 #include <math.h>
 #include <sys/time.h>
 
-#ifdef sun    // Solaris x86
-#include <sys/types.h>
-#include <sys/ucontext.h>
-#endif
-
 #ifdef HAVE_VALGRIND_MEMCHECK_H
 #include <valgrind/memcheck.h>
 #endif
@@ -79,6 +74,8 @@
 #define MONO_IS_COND_BRANCH_OP(ins) (((ins)->opcode >= CEE_BEQ && (ins)->opcode <= CEE_BLT_UN) || ((ins)->opcode >= OP_LBEQ && (ins)->opcode <= OP_LBLT_UN) || ((ins)->opcode >= OP_FBEQ && (ins)->opcode <= OP_FBLT_UN) || ((ins)->opcode >= OP_IBEQ && (ins)->opcode <= OP_IBLT_UN))
 #define MONO_IS_COND_BRANCH_NOFP(ins) (MONO_IS_COND_BRANCH_OP(ins) && (ins)->inst_left->inst_left->type != STACK_R8)
 
+#define MONO_IS_BRANCH_OP(ins) (MONO_IS_COND_BRANCH_OP(ins) || ((ins)->opcode == CEE_BR) || ((ins)->opcode == OP_BR_REG) || ((ins)->opcode == CEE_SWITCH))
+
 #define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
 
 static void setup_stat_profiler (void);
@@ -429,12 +426,13 @@ mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
                (dest)->type = STACK_PTR;       \
     } while (0)
 
-#define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type) do {    \
+#define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type,stack_class) do { \
                (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
                (dest)->opcode = OP_AOTCONST;   \
                (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token));  \
                (dest)->inst_p1 = (gpointer)(patch_type); \
                (dest)->type = (stack_type);    \
+        (dest)->klass = (stack_class);          \
     } while (0)
 
 #endif
@@ -2952,8 +2950,14 @@ mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
        if (!(cfg->opt & MONO_OPT_SHARED)) {
                vtable = mono_class_vtable (cfg->domain, method->klass);
                if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
-                       if (cfg->run_cctors)
+                       if (cfg->run_cctors) {
+                               /* This makes so that inline cannot trigger */
+                               /* .cctors: too many apps depend on them */
+                               /* running with a specific order... */
+                               if (! vtable->initialized)
+                                       return FALSE;
                                mono_runtime_class_init (vtable);
+                       }
                }
                else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
                        return FALSE;
@@ -3214,8 +3218,8 @@ mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *s
                sp++;
        }
 }
-#define MONO_INLINE_CALLED_LIMITED_METHODS 1
-#define MONO_INLINE_CALLER_LIMITED_METHODS 1
+#define MONO_INLINE_CALLED_LIMITED_METHODS 0
+#define MONO_INLINE_CALLER_LIMITED_METHODS 0
 
 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
 static char*
@@ -3531,12 +3535,22 @@ mini_get_class (MonoMethod *method, guint32 token, MonoGenericContext *context)
        return klass;
 }
 
+/*
+ * Returns TRUE if the JIT should abort inlining because "callee"
+ * is influenced by security attributes.
+ */
 static
-void check_linkdemand (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee, MonoBasicBlock *bblock, unsigned char *ip)
+gboolean check_linkdemand (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee, MonoBasicBlock *bblock, unsigned char *ip)
 {
-       guint32 result = mono_declsec_linkdemand (cfg->domain, caller, callee);
+       guint32 result;
+       
+       if ((cfg->method != caller) && mono_method_has_declsec (callee)) {
+               return TRUE;
+       }
+       
+       result = mono_declsec_linkdemand (cfg->domain, caller, callee);
        if (result == MONO_JIT_SECURITY_OK)
-               return;
+               return FALSE;
 
        if (result == MONO_JIT_LINKDEMAND_ECMA) {
                /* Generate code to throw a SecurityException before the actual call/link */
@@ -3555,6 +3569,8 @@ void check_linkdemand (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee,
                cfg->exception_type = MONO_EXCEPTION_SECURITY_LINKDEMAND;
                cfg->exception_data = result;
        }
+       
+       return FALSE;
 }
 
 static gboolean
@@ -3700,8 +3716,9 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
        dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE;
        dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH;
        dont_verify |= method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE; /* bug #77896 */
+       dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP;
 
-       /* still some type unsefety issues in marshal wrappers... (unknown is PtrToStructure) */
+       /* still some type unsafety issues in marshal wrappers... (unknown is PtrToStructure) */
        dont_verify_stloc = method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE;
        dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_UNKNOWN;
        dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED;
@@ -4328,7 +4345,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                goto load_error;
 
                        if (mono_use_security_manager) {
-                               check_linkdemand (cfg, method, cmethod, bblock, ip);
+                               if (check_linkdemand (cfg, method, cmethod, bblock, ip))
+                                       INLINE_FAILURE;
                        }
 
                        ins->inst_p0 = cmethod;
@@ -4394,7 +4412,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                n = fsig->param_count + fsig->hasthis;
 
                                if (mono_use_security_manager) {
-                                       check_linkdemand (cfg, method, cmethod, bblock, ip);
+                                       if (check_linkdemand (cfg, method, cmethod, bblock, ip))
+                                               INLINE_FAILURE;
                                }
 
                                if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
@@ -4490,12 +4509,14 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                }
 
                                ip += 5;
+                               ins_flag = 0;
                                break;
                        }
 
                        if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) &&
                                 (mono_metadata_signature_equal (mono_method_signature (method), mono_method_signature (cmethod)))) {
                                int i;
+
                                /* Prevent inlining of methods with tail calls (the call stack would be altered) */
                                INLINE_FAILURE;
                                /* FIXME: This assumes the two methods has the same number and type of arguments */
@@ -4540,6 +4561,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                }
 
                                ip += 5;
+                               ins_flag = 0;
                                break;
                        }
 
@@ -4577,6 +4599,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        bblock = ebblock;
 
                                        inline_costs += costs;
+                                       ins_flag = 0;
                                        break;
                                }
                        }
@@ -4615,7 +4638,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                                ip += 6;
                                        else
                                                ip += 5;
-
+                                       ins_flag = 0;
                                        break;
                                }
                        }
@@ -4702,6 +4725,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        }
 
                        ip += 5;
+                       ins_flag = 0;
                        break;
                }
                case CEE_RET:
@@ -5308,7 +5332,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                goto load_error;
 
                        if (mono_use_security_manager) {
-                               check_linkdemand (cfg, method, cmethod, bblock, ip);
+                               if (check_linkdemand (cfg, method, cmethod, bblock, ip))
+                                       INLINE_FAILURE;
                        }
 
                        n = fsig->param_count;
@@ -5900,8 +5925,14 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                                        g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
                                                class_inits = g_slist_prepend (class_inits, vtable);
                                        } else {
-                                               if (cfg->run_cctors)
+                                               if (cfg->run_cctors) {
+                                                       /* This makes so that inline cannot trigger */
+                                                       /* .cctors: too many apps depend on them */
+                                                       /* running with a specific order... */
+                                                       if (! vtable->initialized)
+                                                               INLINE_FAILURE;
                                                        mono_runtime_class_init (vtable);
+                                               }
                                        }
                                        addr = (char*)vtable->data + field->offset;
 
@@ -6880,7 +6911,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                mono_class_init (cmethod->klass);
 
                                if (mono_use_security_manager) {
-                                       check_linkdemand (cfg, method, cmethod, bblock, ip);
+                                       if (check_linkdemand (cfg, method, cmethod, bblock, ip))
+                                               INLINE_FAILURE;
                                }
 
                                handle_loaded_temps (cfg, bblock, stack_start, sp);
@@ -6910,7 +6942,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                mono_class_init (cmethod->klass);
 
                                if (mono_use_security_manager) {
-                                       check_linkdemand (cfg, method, cmethod, bblock, ip);
+                                       if (check_linkdemand (cfg, method, cmethod, bblock, ip))
+                                               INLINE_FAILURE;
                                }
 
                                handle_loaded_temps (cfg, bblock, stack_start, sp);
@@ -8351,6 +8384,36 @@ mono_remove_patch_info (MonoCompile *cfg, int ip)
        }
 }
 
+/**
+ * mono_patch_info_dup_mp:
+ *
+ * Make a copy of PATCH_INFO, allocating memory from the mempool MP.
+ */
+MonoJumpInfo*
+mono_patch_info_dup_mp (MonoMemPool *mp, MonoJumpInfo *patch_info)
+{
+       MonoJumpInfo *res = mono_mempool_alloc (mp, sizeof (MonoJumpInfo));
+       memcpy (res, patch_info, sizeof (MonoJumpInfo));
+
+       switch (patch_info->type) {
+       case MONO_PATCH_INFO_LDSTR:
+       case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
+       case MONO_PATCH_INFO_LDTOKEN:
+       case MONO_PATCH_INFO_DECLSEC:
+               res->data.token = mono_mempool_alloc (mp, sizeof (MonoJumpInfoToken));
+               memcpy (res->data.token, patch_info->data.token, sizeof (MonoJumpInfoToken));
+               break;
+       case MONO_PATCH_INFO_SWITCH:
+               res->data.table = mono_mempool_alloc (mp, sizeof (MonoJumpInfoBBTable));
+               memcpy (res->data.table, patch_info->data.table, sizeof (MonoJumpInfoBBTable));
+               break;
+       default:
+               break;
+       }
+
+       return res;
+}
+
 gpointer
 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
 {
@@ -8744,6 +8807,13 @@ remove_block_if_useless (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *p
        if ((previous_bb == cfg->bb_entry) && (bb->next_bb != target_bb)) {
                return FALSE;
        }
+
+       /* 
+        * Do not touch BBs following a try block as the code in 
+        * mini_method_compile needs them to compute the length of the try block.
+        */
+       if (MONO_BBLOCK_IS_IN_REGION (previous_bb, MONO_REGION_TRY))
+               return FALSE;
        
        /* Check that there is a target BB, and that bb is not an empty loop (Bug 75061) */
        if ((target_bb != NULL) && (target_bb != bb)) {
@@ -8798,6 +8868,11 @@ merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
 
        replace_basic_block (bb, bbn, bb);
 
+       /* Nullify branch at the end of bb */
+       if (bb->last_ins && MONO_IS_BRANCH_OP (bb->last_ins)) {
+               bb->last_ins->opcode = CEE_NOP;
+       }               
+
        if (bb->last_ins) {
                if (bbn->code) {
                        bb->last_ins->next = bbn->code;
@@ -9870,6 +9945,12 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
        if (parts == 3)
                return cfg;
 
+       if (cfg->verbose_level > 4) {
+               printf ("BEFORE DECOMPSE START\n");
+               mono_print_code (cfg);
+               printf ("BEFORE DECOMPSE END\n");
+       }
+       
        decompose_pass (cfg);
 
        if (cfg->got_var) {
@@ -10065,7 +10146,7 @@ mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain, in
                MonoMethod *nm;
                MonoMethodPInvoke* piinfo = (MonoMethodPInvoke *) method;
 
-               if (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE)
+               if (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE && !MONO_CLASS_IS_IMPORT(method->klass))
                        g_error ("Method '%s' in assembly '%s' contains native code and mono can't run it. The assembly was probably created by Managed C++.\n", mono_method_full_name (method, TRUE), method->klass->image->name);
 
                if (!piinfo->addr) {
@@ -10388,10 +10469,6 @@ mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObjec
 #ifdef __sparc
 #define GET_CONTEXT \
     void *ctx = context;
-#elif defined(sun)    // Solaris x86
-#define GET_CONTEXT \
-    ucontext_t *uctx = context; \
-    struct sigcontext *ctx = (struct sigcontext *)&(uctx->uc_mcontext);
 #elif defined (MONO_ARCH_USE_SIGACTION)
 #define GET_CONTEXT \
     void *ctx = context;
@@ -11123,6 +11200,12 @@ mini_cleanup (MonoDomain *domain)
        mono_trace_cleanup ();
 
        mono_counters_dump (-1, stdout);
+
+       TlsFree(mono_jit_tls_id);
+
+       DeleteCriticalSection (&jit_mutex);
+
+       DeleteCriticalSection (&mono_delegate_section);
 }
 
 void