From 40375316e2aab269be9f3ce61c973872b40c8fdf Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Wed, 25 Sep 2019 19:24:01 +0200 Subject: [PATCH] detect some optimization of GOTO jumps --- .../jwebassembly/module/BranchManger.java | 8 ++++++++ .../runtime/ControlFlowOperators.java | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/de/inetsoftware/jwebassembly/module/BranchManger.java b/src/de/inetsoftware/jwebassembly/module/BranchManger.java index e3d1760..3119eb8 100644 --- a/src/de/inetsoftware/jwebassembly/module/BranchManger.java +++ b/src/de/inetsoftware/jwebassembly/module/BranchManger.java @@ -186,6 +186,14 @@ class BranchManger { convertToLoop( parsedBlock, conditionStart, conditionEnd ); allParsedOperations.remove( n ); ((IfParsedBlock)nextBlock).negateCompare(); + + // if conditions that point at the end of the loop for optimization must now point at start. + for( n = b - 1; n >= 0; n-- ) { + ParsedBlock prevBlock = allParsedOperations.get( n ); + if( prevBlock.op == JavaBlockOperator.IF && prevBlock.endPosition == conditionStart ) { + prevBlock.endPosition = parsedBlock.startPosition; + } + } break; } if( nextBlock.op == JavaBlockOperator.GOTO && nextBlock.endPosition == nextPos && n > 1 ) { // Eclipse loop with wide goto_w diff --git a/test/de/inetsoftware/jwebassembly/runtime/ControlFlowOperators.java b/test/de/inetsoftware/jwebassembly/runtime/ControlFlowOperators.java index 0e46a68..a7a2d63 100644 --- a/test/de/inetsoftware/jwebassembly/runtime/ControlFlowOperators.java +++ b/test/de/inetsoftware/jwebassembly/runtime/ControlFlowOperators.java @@ -70,6 +70,7 @@ public class ControlFlowOperators extends AbstractBaseTest { addParam( list, script, "ifAndOr4" ); addParam( list, script, "ifAndOr6" ); addParam( list, script, "ifAndOr8" ); + addParam( list, script, "ifWithoutElseAndLoop" ); } rule.setTestParameters( list ); return list; @@ -449,5 +450,20 @@ public class ControlFlowOperators extends AbstractBaseTest { } return result; } + + private static int ifWithoutElseAndLoop; + + @Export + static int ifWithoutElseAndLoop() { + int n = 1; + // because some compiler (Eclipse) move the loop condition to the end of the loop. Then there can be an optimization that the if jump to the end of the loop. + if( ifWithoutElseAndLoop != 1 ) { + ifWithoutElseAndLoop = 1; + } + while( n < 10 ) { + ifWithoutElseAndLoop *= n++; + } + return ifWithoutElseAndLoop; + } } }