[m-rev.] for review: do not emit class initialization debugging code in Java by default

Julien Fischer jfischer at opturion.com
Fri Aug 2 15:54:17 AEST 2019


For review by anyone.

In particular, should the new option be publicly documented?  I can't see 
that it's of much use except to Mercury developers.

-------------------

Do not emit class initialization debugging code in Java by default.

At the moment, we emit static initializers into generated Java code that print
a trace of class initialization to the standard output at startup.  This diff
add as a new developer only option, --debug-class-init, that controls whether
we emit such debugging code.  The default is now *not* to emit it.

Avoid print a commenting containing the Mercury end_module declaration at the
end of a generated Java class unless --auto-comments is given.

compiler/options.m:
      Add the new option.

      Fix spelling.

compiler/mlds_to_java_util.m:
      Add a function for retrieving the value of --debug-class-init.

compiler/mlds_to_java_file.m:
      Only emit code to trace class initialization if the new option is set.

      Only emit the end_module comment if --auto-comments is set.

Julien.

diff --git a/compiler/mlds_to_java_file.m b/compiler/mlds_to_java_file.m
index a494866..4937dec 100644
--- a/compiler/mlds_to_java_file.m
+++ b/compiler/mlds_to_java_file.m
@@ -339,7 +339,7 @@ output_java_src_file(ModuleInfo, Indent, MLDS, Errors, !IO) :-
              EnvVarNamesSet, !IO)
      ),

-    output_src_end_for_java(Indent, ModuleName, !IO),
+    output_src_end_for_java(Info, Indent, ModuleName, !IO),
      % XXX Need to handle non-Java foreign code at this point.

      Errors = ForeignDeclErrors ++ ForeignCodeErrors.
@@ -590,7 +590,7 @@ output_src_start_for_java(Info, Indent, MercuryModuleName, Imports,
      io.write_string(ClassName, !IO),
      io.write_string(" {\n", !IO),

-    output_debug_class_init(MercuryModuleName, "start", !IO),
+    output_debug_class_init(Info, MercuryModuleName, "start", !IO),

      % Check if this module contains a `main' predicate and if it does insert
      % a `main' method in the resulting Java class that calls the `main'
@@ -635,29 +635,41 @@ write_main_driver_for_java(Indent, ClassName, !IO) :-
      output_n_indents(Indent, !IO),
      io.write_string("}\n", !IO).

-:- pred output_src_end_for_java(indent::in, mercury_module_name::in,
-    io::di, io::uo) is det.
+:- pred output_src_end_for_java(java_out_info::in, indent::in,
+        mercury_module_name::in, io::di, io::uo) is det.

-output_src_end_for_java(Indent, ModuleName, !IO) :-
-    output_debug_class_init(ModuleName, "end", !IO),
+output_src_end_for_java(Info, Indent, ModuleName, !IO) :-
+    output_debug_class_init(Info, ModuleName, "end", !IO),
      io.write_string("}\n", !IO),
-    output_n_indents(Indent, !IO),
-    io.write_string("// :- end_module ", !IO),
-    prog_out.write_sym_name(ModuleName, !IO),
-    io.write_string(".\n", !IO).
+    AutoComments = Info ^ joi_auto_comments,
+    (
+        AutoComments = yes,
+        output_n_indents(Indent, !IO),
+        io.write_string("// :- end_module ", !IO),
+        prog_out.write_sym_name(ModuleName, !IO),
+        io.write_string(".\n", !IO)
+    ;
+        AutoComments = no
+    ).

-:- pred output_debug_class_init(mercury_module_name::in, string::in,
-    io::di, io::uo) is det.
+:- pred output_debug_class_init(java_out_info::in, mercury_module_name::in,
+    string::in, io::di, io::uo) is det.

-output_debug_class_init(ModuleName, State, !IO) :-
-    list.foldl(io.write_string, [
+output_debug_class_init(Info, ModuleName, State, !IO) :-
+    DebugClassInit = get_debug_class_init(Info),
+    (
+        DebugClassInit = yes,
+        list.foldl(io.write_string, [
          "  static {\n",
          "    if (System.getenv(""MERCURY_DEBUG_CLASS_INIT"") != null) {\n",
          "      System.out.println(""[", sym_name_mangle(ModuleName),
          " ", State, " init]"");\n",
          "    }\n",
          "  }\n"
-    ], !IO).
+        ], !IO)
+    ;
+        DebugClassInit = no
+    ).

  %---------------------------------------------------------------------------%
  :- end_module ml_backend.mlds_to_java_file.
diff --git a/compiler/mlds_to_java_util.m b/compiler/mlds_to_java_util.m
index d543123..5351c45 100644
--- a/compiler/mlds_to_java_util.m
+++ b/compiler/mlds_to_java_util.m
@@ -56,6 +56,8 @@
  :- func init_java_out_info(module_info, string,
      map(mlds_code_addr, code_addr_wrapper)) = java_out_info.

+:- func get_debug_class_init(java_out_info) = bool.
+
  %---------------------------------------------------------------------------%

  :- type context_marker
@@ -117,6 +119,13 @@ init_java_out_info(ModuleInfo, SourceFileName, AddrOfMap) = Info :-
          LineNumbers, ForeignLineNumbers, MLDS_ModuleName, SourceFileName,
          AddrOfMap, do_not_output_generics, bc_none, []).

+get_debug_class_init(Info) = DebugClassInit :-
+    % It's not worth having an extra fieldin the java_out_info struct for
+    % this since we only look it up twice per module.
+    ModuleInfo = Info ^ joi_module_info,
+    module_info_get_globals(ModuleInfo, Globals),
+    lookup_bool_option(Globals, debug_class_init, DebugClassInit).
+
  %---------------------------------------------------------------------------%

  :- mutable(last_context, prog_context, context_init, ground,
diff --git a/compiler/options.m b/compiler/options.m
index 62bbb58..a94d12d 100644
--- a/compiler/options.m
+++ b/compiler/options.m
@@ -39,7 +39,7 @@
  % - Every option should have a clause in the long_options predicate
  %   that converts the user-visible name of the option into its internal
  %   representation as a value in the options type. For options whose names
-%   include words that whose spelling differs in American vs British english,
+%   include words that whose spelling differs in American vs British English,
  %   there should normally be one entry for each spelling. In the rare case
  %   that the option is used very frequently, there may also be an entry
  %   for the option in the short_option predicate.
@@ -696,6 +696,7 @@
      ;       prefer_while_loop_over_jump_self
      ;       prefer_while_loop_over_jump_mutual
      ;       opt_no_return_calls
+    ;       debug_class_init

      % Optimization Options
      ;       opt_level
@@ -1590,7 +1591,8 @@ option_defaults_2(code_gen_option, [
      prefer_switch                       -   bool(yes),
      prefer_while_loop_over_jump_self    -   bool(yes),
      prefer_while_loop_over_jump_mutual  -   bool(no),
-    opt_no_return_calls                 -   bool(yes)
+    opt_no_return_calls                 -   bool(yes),
+    debug_class_init                    -   bool(no)
  ]).
  option_defaults_2(special_optimization_option, [
      % Special optimization options.
@@ -2537,6 +2539,7 @@ long_option("prefer-while-loop-over-jump-self",
  long_option("prefer-while-loop-over-jump-mutual",
                                      prefer_while_loop_over_jump_mutual).
  long_option("opt-no-return-calls",  opt_no_return_calls).
+long_option("debug-class-init",     debug_class_init).

  % optimization options

@@ -4899,7 +4902,7 @@ options_help_compilation_model -->
          % RBMM is undocumented since it is still experimental.
          % should also document rbmmd rbmmp rbmmdp
          %"--use-regions\t\t(grade modifier: `.rbmm')",
-        %"\tEnable support for region-based memory managment."
+        %"\tEnable support for region-based memory management."
          %"--use-alloc-regions",
          %"\tCompute and use the exact set of regions",
          %"\t that may be allocated into by a call."
@@ -5024,7 +5027,7 @@ options_help_compilation_model -->
  %        "\tDo not box 64-bit integer numbers",
  %        "\tThis assumes that word size of the target machine is at least",
  %        "\t64-bits in size.",
-%        "\tThe C code needs to be compiled iwth `-UMR_BOXED_INT64S'.",
+%        "\tThe C code needs to be compiled with `-UMR_BOXED_INT64S'.",

          % This is a developer only option.
  %       "--no-unboxed-no-tag-types",
@@ -5253,6 +5256,11 @@ options_help_code_generation -->
  %       "--no-opt-no-return-calls",
  %       "\tDo not optimize the stack usage of calls that cannot return.",

+        % This is a developer only option.
+%        "--debug-class-init",
+%        "\tIn Java grades, generate code that causes a trace of class",
+%        "\tinitialization to be printed to the standard output when the",
+%        "\tenvironment variable MERCURY_DEBUG_CLASS_INIT is defined."
      ]),

      io.write_string("\n    Code generation target options:\n"),
@@ -5585,7 +5593,7 @@ options_help_hlds_hlds_optimization -->
  %        "\tEnable the analysis for region-based memory management."
      ]).

-    % XXX This is out-of-date. --smart-indxing also affects the
+    % XXX This is out-of-date. --smart-indexing also affects the
      % MLDS backend.
      %
  :- pred options_help_hlds_llds_optimization(io::di, io::uo) is det.


More information about the reviews mailing list