[m-rev.] for review: setting the reserve stack size on Windows

Julien Fischer jfischer at opturion.com
Wed Apr 10 13:36:28 AEST 2013


For review by anyone.

Add an option for controlling the C stack size on Windows.

Add a new option, --cstack-reserve-size, that specifies the amount of virtual
memory that should be reserved for the stack in executables on Windows systems.
This is useful for non-trivial programs that use the high-level C backend since
they often bump into the default 1Mb limit.

compiler/options.m::
	Add the new option.

compiler/compile_target_code.m:
	Pass the appropriate flags to the linker if the new option is given.

NEWS:
	Announce the new option.

Julien.

diff --git a/NEWS b/NEWS
index a536ebe..722d792 100644
--- a/NEWS
+++ b/NEWS
@@ -15,6 +15,9 @@ Changes to the Mercury compiler:
 * A new option, `--sign-assembly', provides supports for signing
   assemblies generated by the C# backend with a strong name.

+* A new option, `--cstack-reserve-size', allows the size of the C
+  stack for executables to be change on Microsoft Windows systems.
+

 NEWS for Mercury 12.08
 ----------------------
diff --git a/compiler/compile_target_code.m b/compiler/compile_target_code.m
index 1c1c453..d6ddfdc 100644
--- a/compiler/compile_target_code.m
+++ b/compiler/compile_target_code.m
@@ -1909,7 +1909,8 @@ link_exe_or_shared_lib(Globals, ErrorStream,
LinkTargetType, ModuleName,
             AllowUndef = no,
             globals.lookup_string_option(Globals,
                 linker_error_undefined_flag, UndefOpt)
-        )
+        ),
+        ReserveStackSizeOpt = ""
     ;
         LinkTargetType = executable,
         CommandOpt = link_executable_command,
@@ -1919,7 +1920,8 @@ link_exe_or_shared_lib(Globals, ErrorStream,
LinkTargetType, ModuleName,
         ThreadFlagsOpt = linker_thread_flags,
         DebugFlagsOpt = linker_debug_flags,
         TraceFlagsOpt = linker_trace_flags,
-        UndefOpt = ""
+        UndefOpt = "",
+        ReserveStackSizeOpt = reserve_stack_size_flags(Globals)
     ),

     % Should the executable be stripped?
@@ -2083,6 +2085,7 @@ link_exe_or_shared_lib(Globals, ErrorStream,
LinkTargetType, ModuleName,
                     UndefOpt, " ",
                     ThreadOpts, " ",
                     TraceOpts, " ",
+                    ReserveStackSizeOpt, " ",
                     OutputOpt, quote_arg(OutputFileName), " ",
                     Objects, " ",
                     LinkOptSep, " ",
@@ -2665,6 +2668,27 @@ get_linker_output_option(Globals,
LinkTargetType, OutputOpt) :-
         OutputOpt = " -o "
     ).

+:- func reserve_stack_size_flags(globals) = string.
+
+reserve_stack_size_flags(Globals) = Flags :-
+    globals.lookup_int_option(Globals, cstack_reserve_size, ReserveStackSize),
+    ( if ReserveStackSize = -1 then
+        Flags = ""
+    else
+        get_c_compiler_type(Globals, C_CompilerType),
+        (
+            ( C_CompilerType = cc_gcc(_, _, _)
+            ; C_CompilerType = cc_clang(_)
+            ; C_CompilerType = cc_lcc
+            ; C_CompilerType = cc_unknown
+            ),
+            string.format("-Wl,--stack=%d", [i(ReserveStackSize)], Flags)
+        ;
+            C_CompilerType = cc_cl(_),
+            string.format("-stack:%d", [i(ReserveStackSize)], Flags)
+        )
+    ).
+
 %-----------------------------------------------------------------------------%

 :- pred process_link_library(globals::in, list(dir_name)::in, string::in,
diff --git a/compiler/options.m b/compiler/options.m
index d9d1f62..ed54acf 100644
--- a/compiler/options.m
+++ b/compiler/options.m
@@ -915,6 +915,7 @@
     ;       frameworks
     ;       framework_directories
     ;       sign_assembly
+    ;       cstack_reserve_size

     % Auto-configured options.
     ;       shared_library_extension
@@ -1817,6 +1818,7 @@ option_defaults_2(link_option, [
     frameworks                          -   accumulating([]),
     framework_directories               -   accumulating([]),
     sign_assembly                       -   string(""),
+    cstack_reserve_size                 -   int(-1),

     shared_library_extension            -   string(".so"),
                                         % The `mmc' script will override the
@@ -2789,6 +2791,7 @@ long_option("extra-inits",
extra_initialization_functions).
 long_option("framework",        frameworks).
 long_option("framework-directory", framework_directories).
 long_option("sign-assembly", sign_assembly).
+long_option("cstack-reserve-size", cstack_reserve_size).

 long_option("shared-library-extension", shared_library_extension).
 long_option("library-extension",    library_extension).
@@ -5677,7 +5680,12 @@ options_help_link -->
         "\tSign the current assembly with the strong name contained",
         "\tin the specified key file.",
         "\t(This option is only meaningful when generating library assemblies",
-        "\twith the C# backend.)"
+        "\twith the C# backend.)",
+
+        "--cstack-reserve-size <size>",
+        "\tSet the total size of the C stack in virtual memory for excutables."
+        "\tThe stack size is given in bytes.",
+        "\t(Microsoft Windows only.)"

         % The --shared-library-extension,
         % --library-extension, --executable-file-extension
diff --git a/doc/user_guide.texi b/doc/user_guide.texi
index 1488669..189f402 100644
--- a/doc/user_guide.texi
+++ b/doc/user_guide.texi
@@ -10111,6 +10111,14 @@ specified key file.
 (This option is only meaningful when generating library assemblies
 with the C# backend.)

+ at sp 1
+ at item --cstack-reserve-size @var{size}
+ at findex --cstack-reserve-size
+Set the total size of the C stack in virtual memory for executables.
+The stack size is given in bytes.
+This option is only supported (and indeed only necessary) on systems running
+Microsoft Windows.
+
 @end table

 @c ----------------------------------------------------------------------------



More information about the reviews mailing list