[m-rev.] for review: avoid (some) redundant levels of nesting in generated Java

Julien Fischer jfischer at opturion.com
Sun Sep 15 18:36:00 AEST 2019


For review by anyone.

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

Avoid (some) redundant levels of nesting in generated Java.

compiler/ml_optimize.m:
     After peephole optimization of a block, if the block consists only of
     a single statement, then replace the block by the statement itself.

compiler/mlds_to_java_func.m:
     Do not output curly brackets around an MLDS function body that is a block,
     since that block will be output with its own curly brackets.

Julien.

diff --git a/compiler/ml_optimize.m b/compiler/ml_optimize.m
index 4b83a22..a5e91b6 100644
--- a/compiler/ml_optimize.m
+++ b/compiler/ml_optimize.m
@@ -144,7 +144,15 @@ optimize_in_stmt(OptInfo, Stmt0, Stmt) :-
          maybe_flatten_block(SubStmts2, SubStmts3),
          optimize_in_stmts(OptInfo, SubStmts3, SubStmts),
          % XXX We should also optimize in FuncDefns.
-        Stmt = ml_stmt_block(LocalVarDefns, FuncDefns, SubStmts, Context)
+        ( if
+            LocalVarDefns = [],
+            FuncDefns = [],
+            SubStmts = [SubStmt]
+        then
+            Stmt = SubStmt
+        else
+            Stmt = ml_stmt_block(LocalVarDefns, FuncDefns, SubStmts, Context)
+        )
      ;
          Stmt0 = ml_stmt_while(Kind, Rval, SubStmts0, LocalLoopVars, Context),
          optimize_in_stmt(OptInfo, SubStmts0, SubStmts),
diff --git a/compiler/mlds_to_java_func.m b/compiler/mlds_to_java_func.m
index 2de246b..437a0c5 100644
--- a/compiler/mlds_to_java_func.m
+++ b/compiler/mlds_to_java_func.m
@@ -163,15 +163,21 @@ output_func_for_java(Info, Indent, FuncName, OutputAux, Context, Signature,
          % The signature above will be printed inside a comment.
      ;
          MaybeBody = body_defined_here(Body),
-        indent_line_after_context(Info ^ joi_line_numbers, marker_comment,
-            Context, Indent, !IO),
-        io.write_string("{\n", !IO),
          FuncInfo = func_info_csj(Signature),
-        output_statement_for_java(Info, Indent + 1, FuncInfo, Body,
-            _ExitMethods, !IO),
-        indent_line_after_context(Info ^ joi_line_numbers, marker_comment,
-            Context, Indent, !IO),
-        io.write_string("}\n", !IO)
+        % Do not place redundant brackets around a block.
+        ( if Body = ml_stmt_block(_, _, _, _) then
+            output_statement_for_java(Info, Indent, FuncInfo, Body,
+                _ExitMethods, !IO),
+            indent_line_after_context(Info ^ joi_line_numbers, marker_comment,
+                Context, Indent, !IO)
+        else
+            io.write_string("{\n", !IO),
+            output_statement_for_java(Info, Indent + 1, FuncInfo, Body,
+                _ExitMethods, !IO),
+            indent_line_after_context(Info ^ joi_line_numbers, marker_comment,
+                Context, Indent, !IO),
+            io.write_string("}\n", !IO)
+        )
      ).

  %---------------------------------------------------------------------------%


More information about the reviews mailing list