Skip to content

Commit

Permalink
rewrite simple if exitwhen
Browse files Browse the repository at this point in the history
  • Loading branch information
Frotty committed May 25, 2018
1 parent bfe3fa0 commit 2e3955c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,27 @@ private void optimizeExitwhen(ImExitwhen imExitwhen) {

}

/**
* Rewrites if statements that only contain an exitwhen statement
* so that the if's condition is combined with the exitwhen
* <p>
* if expr1
* exitwhen expr2
* <p>
* to:
* <p>
* exitwhen expr1 and expr2
*/
private void optimizeIfExitwhen(ImIf imIf) {
ImExitwhen imStmt = (ImExitwhen) imIf.getThenBlock().get(0);
imStmt.getCondition().setParent(null);
imIf.getCondition().setParent(null);

imStmt.setCondition((JassIm.ImOperatorCall(WurstOperator.AND, JassIm.ImExprs(imIf.getCondition(), imStmt.getCondition()))));
imStmt.setParent(null);
imIf.replaceBy(imStmt);
}

private void optimizeOpCall(ImOperatorCall opc) {
// Binary
boolean wasViable = true;
Expand Down Expand Up @@ -488,19 +509,21 @@ private void optimizeIf(ImIf imIf) {
totalRewrites++;
}
}
} else if (imIf.getElseBlock().isEmpty() && imIf.getThenBlock().size() == 1 && imIf.getThenBlock().get(0) instanceof ImExitwhen) {
optimizeIfExitwhen(imIf);
}
}

/**
* Optimizes
*
* set x = expr1
* set x = x ⊕ expr2
*
* <p>
* set x = expr1
* set x = x ⊕ expr2
* <p>
* into:
*
* set x = expr1 ⊕ expr2
*
* <p>
* set x = expr1 ⊕ expr2
* <p>
* like code that is created by the branch merger
*/
private void optimizeConsecutiveSet(ImSet imSet1, ImSet imSet2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,4 +625,21 @@ public void optimizeSet2() {
);
}

@Test
public void optimizeExitwhen() {
testAssertOkLines(true,
"package Test",
"native testSuccess()",
"var x = 100",
"init",
" while x > 0",
" if x == 50",
" break",
" if x == 101",
" break",
" x--",
" testSuccess()"
);
}

}

0 comments on commit 2e3955c

Please sign in to comment.