mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 08:14:09 -08:00
Eliminate unnecessary return statements
This commit is contained in:
parent
fa0e884931
commit
e992297781
|
@ -0,0 +1,78 @@
|
|||
/*
|
||||
* Copyright 2017 Alexey Andreev.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.teavm.ast;
|
||||
|
||||
public class AbstractStatementVisitor implements StatementVisitor {
|
||||
@Override
|
||||
public void visit(AssignmentStatement statement) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(SequentialStatement statement) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ConditionalStatement statement) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(SwitchStatement statement) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(WhileStatement statement) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(BlockStatement statement) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(BreakStatement statement) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ContinueStatement statement) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ReturnStatement statement) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ThrowStatement statement) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(InitClassStatement statement) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(TryCatchStatement statement) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(GotoPartStatement statement) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(MonitorEnterStatement statement) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(MonitorExitStatement statement) {
|
||||
}
|
||||
}
|
|
@ -15,10 +15,6 @@
|
|||
*/
|
||||
package org.teavm.ast;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Alexey Andreev
|
||||
*/
|
||||
public interface StatementVisitor {
|
||||
void visit(AssignmentStatement statement);
|
||||
|
||||
|
|
|
@ -47,8 +47,8 @@ public class Optimizer {
|
|||
method.getVariables().clear();
|
||||
method.getVariables().addAll(unusedEliminator.getReorderedVariables());
|
||||
|
||||
RedundantLabelEliminator labelEliminator = new RedundantLabelEliminator();
|
||||
method.getBody().acceptVisitor(labelEliminator);
|
||||
method.getBody().acceptVisitor(new RedundantLabelEliminator());
|
||||
method.getBody().acceptVisitor(new RedundantReturnElimination());
|
||||
|
||||
for (int i = 0; i < method.getVariables().size(); ++i) {
|
||||
method.getVariables().get(i).setIndex(i);
|
||||
|
@ -84,9 +84,12 @@ public class Optimizer {
|
|||
method.getVariables().addAll(unusedEliminator.getReorderedVariables());
|
||||
|
||||
RedundantLabelEliminator labelEliminator = new RedundantLabelEliminator();
|
||||
RedundantReturnElimination returnElimination = new RedundantReturnElimination();
|
||||
for (AsyncMethodPart part : method.getBody()) {
|
||||
part.getStatement().acceptVisitor(labelEliminator);
|
||||
part.getStatement().acceptVisitor(returnElimination);
|
||||
}
|
||||
|
||||
for (int i = 0; i < method.getVariables().size(); ++i) {
|
||||
method.getVariables().get(i).setIndex(i);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* Copyright 2017 Alexey Andreev.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.teavm.ast.optimization;
|
||||
|
||||
import java.util.List;
|
||||
import org.teavm.ast.AbstractStatementVisitor;
|
||||
import org.teavm.ast.BlockStatement;
|
||||
import org.teavm.ast.ConditionalStatement;
|
||||
import org.teavm.ast.ReturnStatement;
|
||||
import org.teavm.ast.SequentialStatement;
|
||||
import org.teavm.ast.Statement;
|
||||
import org.teavm.ast.SwitchClause;
|
||||
import org.teavm.ast.SwitchStatement;
|
||||
import org.teavm.ast.TryCatchStatement;
|
||||
|
||||
class RedundantReturnElimination extends AbstractStatementVisitor {
|
||||
@Override
|
||||
public void visit(SequentialStatement statement) {
|
||||
handleList(statement.getSequence());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ConditionalStatement statement) {
|
||||
handleList(statement.getConsequent());
|
||||
handleList(statement.getAlternative());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(SwitchStatement statement) {
|
||||
handleList(statement.getDefaultClause());
|
||||
for (SwitchClause clause : statement.getClauses()) {
|
||||
handleList(clause.getBody());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(BlockStatement statement) {
|
||||
handleList(statement.getBody());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(TryCatchStatement statement) {
|
||||
handleList(statement.getProtectedBody());
|
||||
handleList(statement.getHandler());
|
||||
}
|
||||
|
||||
private void handleList(List<Statement> statements) {
|
||||
if (statements.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
Statement last = statements.get(statements.size() - 1);
|
||||
if (last instanceof ReturnStatement && ((ReturnStatement) last).getResult() == null) {
|
||||
statements.remove(statements.size() - 1);
|
||||
} else {
|
||||
last.acceptVisitor(this);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user