[mini] Implement float <-> int data transfer instructions in the LLVM backend.
[mono.git] / mono / mini / driver.c
index df7e57641752915c2a304034b438c3ead174bd21..3d179097328ffcc8d4bf731f00f5258af7a42cfb 100644 (file)
@@ -10,7 +10,9 @@
  */
 
 #include <config.h>
+#ifdef HAVE_SIGNAL_H
 #include <signal.h>
+#endif
 #if HAVE_SCHED_SETAFFINITY
 #include <sched.h>
 #endif
@@ -57,7 +59,7 @@
 #include "version.h"
 #include "debugger-agent.h"
 
-static FILE *mini_stats_fd = NULL;
+static FILE *mini_stats_fd;
 
 static void mini_usage (void);
 
@@ -137,6 +139,7 @@ extern char *nacl_mono_path;
     MONO_OPT_CMOV |  \
        MONO_OPT_GSHARED |      \
        MONO_OPT_SIMD | \
+       MONO_OPT_ALIAS_ANALYSIS | \
        MONO_OPT_AOT)
 
 #define EXCLUDED_FROM_ALL (MONO_OPT_SHARED | MONO_OPT_PRECOMP | MONO_OPT_UNSAFE | MONO_OPT_GSHAREDVT)
@@ -292,7 +295,7 @@ opt_descr (guint32 flags) {
 
        need_comma = 0;
        for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
-               if (flags & (1 << i)) {
+               if (flags & (1 << i) && optflag_get_name (i)) {
                        if (need_comma)
                                g_string_append_c (str, ',');
                        g_string_append (str, optflag_get_name (i));
@@ -309,16 +312,19 @@ opt_sets [] = {
        MONO_OPT_BRANCH,
        MONO_OPT_CFOLD,
        MONO_OPT_FCMOV,
+       MONO_OPT_ALIAS_ANALYSIS,
 #ifdef MONO_ARCH_SIMD_INTRINSICS
        MONO_OPT_SIMD,
        MONO_OPT_SSE2,
        MONO_OPT_SIMD | MONO_OPT_SSE2,
 #endif
        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_INTRINS,
+       MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_INTRINS | MONO_OPT_ALIAS_ANALYSIS,
        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS,
        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP,
        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_CFOLD,
        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE,
+       MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_ALIAS_ANALYSIS,
        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS,
        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS | MONO_OPT_TAILC,
        MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS | MONO_OPT_SSA,
@@ -382,7 +388,7 @@ mini_regression_step (MonoImage *image, int verbose, int *total_run, int *total,
                        run++;
                        start_time = g_timer_elapsed (timer, NULL);
                        comp_time -= start_time;
-                       cfg = mini_method_compile (method, mono_get_optimizations_for_method (method, opt_flags), mono_get_root_domain (), TRUE, FALSE, 0);
+                       cfg = mini_method_compile (method, mono_get_optimizations_for_method (method, opt_flags), mono_get_root_domain (), JIT_FLAG_RUN_CCTORS, 0);
                        comp_time += g_timer_elapsed (timer, NULL);
                        if (cfg->exception_type == MONO_EXCEPTION_NONE) {
                                if (verbose >= 2)
@@ -404,8 +410,7 @@ mini_regression_step (MonoImage *image, int verbose, int *total_run, int *total,
 
                        } else {
                                cfailed++;
-                               if (verbose)
-                                       g_print ("Test '%s' failed compilation.\n", method->name);
+                               g_print ("Test '%s' failed compilation.\n", method->name);
                        }
                        if (mini_stats_fd)
                                fprintf (mini_stats_fd, "%f, ",
@@ -481,7 +486,7 @@ mini_regression (MonoImage *image, int verbose, int *total_run)
 
        total = 0;
        *total_run = 0;
-       if (do_single_method_regression) {
+       if (mono_do_single_method_regression) {
                GSList *iter;
 
                mini_regression_step (image, verbose, total_run, &total,
@@ -489,14 +494,14 @@ mini_regression (MonoImage *image, int verbose, int *total_run)
                                timer, domain);
                if (total)
                        return total;
-               g_print ("Single method regression: %d methods\n", g_slist_length (single_method_list));
+               g_print ("Single method regression: %d methods\n", g_slist_length (mono_single_method_list));
 
-               for (iter = single_method_list; iter; iter = g_slist_next (iter)) {
+               for (iter = mono_single_method_list; iter; iter = g_slist_next (iter)) {
                        char *method_name;
 
-                       current_single_method = iter->data;
+                       mono_current_single_method = iter->data;
 
-                       method_name = mono_method_full_name (current_single_method, TRUE);
+                       method_name = mono_method_full_name (mono_current_single_method, TRUE);
                        g_print ("Current single method: %s\n", method_name);
                        g_free (method_name);
 
@@ -931,7 +936,7 @@ compile_all_methods_thread_main_inner (CompileAllThreadArgs *args)
                        g_print ("Compiling %d %s\n", count, desc);
                        g_free (desc);
                }
-               cfg = mini_method_compile (method, mono_get_optimizations_for_method (method, args->opts), mono_get_root_domain (), FALSE, FALSE, 0);
+               cfg = mini_method_compile (method, mono_get_optimizations_for_method (method, args->opts), mono_get_root_domain (), 0, 0);
                if (cfg->exception_type != MONO_EXCEPTION_NONE) {
                        printf ("Compilation of %s failed with exception '%s':\n", mono_method_full_name (cfg->method, TRUE), cfg->exception_message);
                        fail_count ++;
@@ -1282,9 +1287,6 @@ static const char info[] =
 #ifdef MONO_BIG_ARRAYS
        "bigarrays "
 #endif
-#ifdef MONO_DEBUGGER_SUPPORTED
-       "debugger "
-#endif
 #if defined(MONO_ARCH_SOFT_DEBUG_SUPPORTED) && !defined(DISABLE_SOFT_DEBUG)
        "softdebug "
 #endif
@@ -1307,7 +1309,6 @@ static const char info[] =
 #ifdef HOST_WIN32
 BOOL APIENTRY DllMain (HMODULE module_handle, DWORD reason, LPVOID reserved)
 {
-       int dummy;
        if (!mono_gc_dllmain (module_handle, reason, reserved))
                return FALSE;
 
@@ -1321,7 +1322,7 @@ BOOL APIENTRY DllMain (HMODULE module_handle, DWORD reason, LPVOID reserved)
                        FreeLibrary (coree_module_handle);
                break;
        case DLL_THREAD_DETACH:
-               mono_thread_info_dettach ();
+               mono_thread_info_detach ();
                break;
        
        }
@@ -1517,9 +1518,6 @@ mono_main (int argc, char* argv[])
        if (g_getenv ("MONO_NO_SMP"))
                mono_set_use_smp (FALSE);
        
-       if (!g_thread_supported ())
-               g_thread_init (NULL);
-
        g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
        g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
 
@@ -1533,7 +1531,7 @@ mono_main (int argc, char* argv[])
                } else if (strncmp (argv [i], "--single-method=", 16) == 0) {
                        char *full_opts = g_strdup_printf ("-all,%s", argv [i] + 16);
                        action = DO_SINGLE_METHOD_REGRESSION;
-                       single_method_regression_opt = parse_optimizations (full_opts);
+                       mono_single_method_regression_opt = parse_optimizations (full_opts);
                        g_free (full_opts);
                } else if (strcmp (argv [i], "--verbose") == 0 || strcmp (argv [i], "-v") == 0) {
                        mini_verbose++;
@@ -1541,7 +1539,7 @@ mono_main (int argc, char* argv[])
                        char *build = mono_get_runtime_build_info ();
                        char *gc_descr;
 
-                       g_print ("Mono Runtime Engine version %s\nCopyright (C) 2002-2013 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com\n", build);
+                       g_print ("Mono JIT compiler version %s\nCopyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com\n", build);
                        g_free (build);
                        g_print (info);
                        gc_descr = mono_gc_get_description ();
@@ -1839,6 +1837,21 @@ mono_main (int argc, char* argv[])
                return 1;
        }
 
+#if !defined(HOST_WIN32) && defined(HAVE_UNISTD_H)
+       /*
+        * If we are not embedded, use the mono runtime executable to run managed exe's.
+        */
+       {
+               char *runtime_path;
+
+               runtime_path = wapi_process_get_path (getpid ());
+               if (runtime_path) {
+                       wapi_process_set_cli_launcher (runtime_path);
+                       g_free (runtime_path);
+               }
+       }
+#endif
+
        if (g_getenv ("MONO_XDEBUG"))
                enable_debugging = TRUE;
 
@@ -1853,13 +1866,12 @@ mono_main (int argc, char* argv[])
 #endif
 #endif
 
-       if ((action == DO_EXEC) && mono_debug_using_mono_debugger ())
-               action = DO_DEBUGGER;
-
        if (mono_compile_aot || action == DO_EXEC || action == DO_DEBUGGER) {
                g_set_prgname (argv[i]);
        }
 
+       mono_counters_init ();
+
        if (enable_profile)
                mono_profiler_load (profile_options);
 
@@ -1884,28 +1896,24 @@ mono_main (int argc, char* argv[])
 
        if (action == DO_DEBUGGER) {
                enable_debugging = TRUE;
-
-#ifdef MONO_DEBUGGER_SUPPORTED
-               mono_debug_init (MONO_DEBUG_FORMAT_DEBUGGER);
-#else
-               g_print ("The Mono Debugger is not supported on this platform.\n");
+               g_print ("The Mono Debugger is no longer supported.\n");
                return 1;
-#endif
        } else if (enable_debugging)
                mono_debug_init (MONO_DEBUG_FORMAT_MONO);
 
-#ifdef MONO_DEBUGGER_SUPPORTED
-       if (enable_debugging) {
-               if ((opt & MONO_OPT_GSHARED) == 0)
-                       mini_debugger_set_attach_ok ();
-       }
-#endif
-
 #ifdef HOST_WIN32
        if (mixed_mode)
                mono_load_coree (argv [i]);
 #endif
 
+       /* Set rootdir before loading config */
+       mono_set_rootdir ();
+
+       /* Parse gac loading options before loading assemblies. */
+       if (mono_compile_aot || action == DO_EXEC || action == DO_DEBUGGER) {
+               mono_config_parse (config_file);
+       }
+
        mono_set_defaults (mini_verbose, opt);
        domain = mini_init (argv [i], forced_version);
 
@@ -1928,7 +1936,7 @@ mono_main (int argc, char* argv[])
        
        switch (action) {
        case DO_SINGLE_METHOD_REGRESSION:
-               do_single_method_regression = TRUE;
+               mono_do_single_method_regression = TRUE;
        case DO_REGRESSION:
                if (mini_regression_list (mini_verbose, argc -i, argv + i)) {
                        g_print ("Regression ERRORS!\n");
@@ -1971,11 +1979,6 @@ mono_main (int argc, char* argv[])
                break;
        }
 
-       /* Parse gac loading options before loading assemblies. */
-       if (mono_compile_aot || action == DO_EXEC || action == DO_DEBUGGER) {
-               mono_config_parse (config_file);
-       }
-
 #ifdef MONO_JIT_INFO_TABLE_TEST
        if (test_jit_info_table)
                jit_info_table_test (domain);
@@ -2001,7 +2004,7 @@ mono_main (int argc, char* argv[])
                        fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
                        fprintf (stderr, "Loaded from: %s\n",
                                mono_defaults.corlib? mono_image_get_filename (mono_defaults.corlib): "unknown");
-                       fprintf (stderr, "Download a newer corlib or a newer runtime at http://www.go-mono.com/daily.\n");
+                       fprintf (stderr, "Download a newer corlib or a newer runtime at http://www.mono-project.com/download.\n");
                        exit (1);
                }
 
@@ -2024,31 +2027,6 @@ mono_main (int argc, char* argv[])
                mono_thread_manage ();
 #endif
 
-       /* 
-        * On unix, WaitForMultipleObjects for threads is implemented by waiting on
-        * a cond variable, which is set by the thread when it exits _mono code_, 
-        * but it could still be running libc code. On amd64, the libc thread exit 
-        * code does a stack unwind, and if it encounters a frame pointing to native
-        * code which is in memory which is no longer mapped (because the runtime has
-        * shut down), it will crash:
-        * http://mail-archives.apache.org/mod_mbox/harmony-dev/200801.mbox/%3C200801130327.41572.gshimansky@apache.org%3E
-        * Testcase: tests/main-exit-background-change.exe.
-        * Testcase: test/main-returns-background-abort-resetabort.exe.
-        * To make this race less frequent, we avoid freeing the global code manager.
-        * Since mono_main () is hopefully only used by the runtime executable, this 
-        * will only cause a shutdown leak. This workaround also has the advantage
-        * that it can be back-ported to 2.0 safely.
-        * FIXME: Fix this properly by waiting for threads to really exit using 
-        * pthread_join (). This cannot be done currently as the io-layer calls
-        * pthread_detach ().
-        *
-        * This used to be an amd64 only crash, but it looks like now most glibc targets do unwinding
-        * that requires reading the target code.
-        */
-#if defined( __linux__ ) || defined( __native_client__ )
-               mono_dont_free_global_codeman = TRUE;
-#endif
-
                mini_cleanup (domain);
 
                /* Look up return value from System.Environment.ExitCode */
@@ -2059,22 +2037,7 @@ mono_main (int argc, char* argv[])
                mini_cleanup (domain);
                return 0;
        } else if (action == DO_DEBUGGER) {
-#ifdef MONO_DEBUGGER_SUPPORTED
-               const char *error;
-
-               error = mono_check_corlib_version ();
-               if (error) {
-                       fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
-                       fprintf (stderr, "Download a newer corlib or a newer runtime at http://www.go-mono.com/daily.\n");
-                       exit (1);
-               }
-
-               mini_debugger_main (domain, assembly, argc - i, argv + i);
-               mini_cleanup (domain);
-               return 0;
-#else
                return 1;
-#endif
        }
        desc = mono_method_desc_new (mname, 0);
        if (!desc) {
@@ -2115,10 +2078,10 @@ mono_main (int argc, char* argv[])
                        (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
                        MonoMethod *nm;
                        nm = mono_marshal_get_native_wrapper (method, TRUE, FALSE);
-                       cfg = mini_method_compile (nm, opt, mono_get_root_domain (), FALSE, FALSE, part);
+                       cfg = mini_method_compile (nm, opt, mono_get_root_domain (), 0, part);
                }
                else
-                       cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, part);
+                       cfg = mini_method_compile (method, opt, mono_get_root_domain (), 0, part);
                if ((mono_graph_options & MONO_GRAPH_CFG_SSA) && !(cfg->comp_done & MONO_COMP_SSA)) {
                        g_warning ("no SSA info available (use -O=deadce)");
                        return 1;
@@ -2150,7 +2113,7 @@ mono_main (int argc, char* argv[])
                                opt = opt_sets [i];
                                g_timer_start (timer);
                                for (j = 0; j < count; ++j) {
-                                       cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, 0);
+                                       cfg = mini_method_compile (method, opt, mono_get_root_domain (), 0, 0);
                                        mono_destroy_compile (cfg);
                                }
                                g_timer_stop (timer);
@@ -2173,12 +2136,12 @@ mono_main (int argc, char* argv[])
                                        (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
                                        method = mono_marshal_get_native_wrapper (method, TRUE, FALSE);
 
-                               cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, 0);
+                               cfg = mini_method_compile (method, opt, mono_get_root_domain (), 0, 0);
                                mono_destroy_compile (cfg);
                        }
                }
        } else {
-               cfg = mini_method_compile (method, opt, mono_get_root_domain (), FALSE, FALSE, 0);
+               cfg = mini_method_compile (method, opt, mono_get_root_domain (), 0, 0);
                mono_destroy_compile (cfg);
        }
 #endif
@@ -2221,6 +2184,8 @@ mono_jit_init_version (const char *domain_name, const char *runtime_version)
 void        
 mono_jit_cleanup (MonoDomain *domain)
 {
+       mono_thread_manage ();
+
        mini_cleanup (domain);
 }
 
@@ -2267,3 +2232,16 @@ mono_set_signal_chaining (gboolean chain_signals)
 {
        mono_do_signal_chaining = chain_signals;
 }
+
+/**
+ * mono_set_crash_chaining:
+ *
+ * Enable/disable crash chaining due to signals. When a fatal signal is delivered and
+ * Mono doesn't know how to handle it, it will invoke the crash handler. If chrash chaining
+ * is enabled, it will first print its crash information and then try to chain with the native handler.
+ */
+void
+mono_set_crash_chaining (gboolean chain_crashes)
+{
+       mono_do_crash_chaining = chain_crashes;
+}