Working on async exception catching

This commit is contained in:
Alexey Andreev 2015-02-02 18:58:44 +04:00
parent 4496b1ab30
commit b36c10760c
7 changed files with 140 additions and 20 deletions

View File

@ -0,0 +1,25 @@
/*
* Copyright 2015 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.javascript;
/**
*
* @author Alexey Andreev
*/
public enum AsyncInvocationType {
COMPLETE,
ERROR
}

View File

@ -203,9 +203,8 @@ public class Decompiler {
AsyncProgramSplitter splitter = new AsyncProgramSplitter(asyncMethods); AsyncProgramSplitter splitter = new AsyncProgramSplitter(asyncMethods);
splitter.split(method.getProgram()); splitter.split(method.getProgram());
for (int i = 0; i < splitter.size(); ++i) { for (int i = 0; i < splitter.size(); ++i) {
AsyncMethodPart part = new AsyncMethodPart(); AsyncMethodPart part = getRegularMethodStatement(splitter.getProgram(i), splitter.getBlockSuccessors(i));
part.setInputVariable(splitter.getInput(i)); part.setInputVariable(splitter.getInput(i));
part.setStatement(getRegularMethodStatement(splitter.getProgram(i), splitter.getBlockSuccessors(i)));
node.getBody().add(part); node.getBody().add(part);
} }
Program program = method.getProgram(); Program program = method.getProgram();
@ -226,7 +225,9 @@ public class Decompiler {
public RegularMethodNode decompileRegularCacheMiss(MethodHolder method) { public RegularMethodNode decompileRegularCacheMiss(MethodHolder method) {
RegularMethodNode methodNode = new RegularMethodNode(method.getReference()); RegularMethodNode methodNode = new RegularMethodNode(method.getReference());
Program program = method.getProgram(); Program program = method.getProgram();
methodNode.setBody(getRegularMethodStatement(program, new int[program.basicBlockCount()])); int[] targetBlocks = new int[program.basicBlockCount()];
Arrays.fill(targetBlocks, -1);
methodNode.setBody(getRegularMethodStatement(program, targetBlocks).getStatement());
for (int i = 0; i < program.variableCount(); ++i) { for (int i = 0; i < program.variableCount(); ++i) {
methodNode.getVariables().add(program.variableAt(i).getRegister()); methodNode.getVariables().add(program.variableAt(i).getRegister());
} }
@ -241,7 +242,8 @@ public class Decompiler {
return methodNode; return methodNode;
} }
private Statement getRegularMethodStatement(Program program, int[] targetBlocks) { private AsyncMethodPart getRegularMethodStatement(Program program, int[] targetBlocks) {
AsyncMethodPart result = new AsyncMethodPart();
lastBlockId = 1; lastBlockId = 1;
graph = ProgramUtils.buildControlFlowGraph(program); graph = ProgramUtils.buildControlFlowGraph(program);
indexer = new GraphIndexer(graph); indexer = new GraphIndexer(graph);
@ -297,6 +299,7 @@ public class Decompiler {
InstructionLocation lastLocation = null; InstructionLocation lastLocation = null;
NodeLocation nodeLocation = null; NodeLocation nodeLocation = null;
List<Instruction> instructions = generator.currentBlock.getInstructions(); List<Instruction> instructions = generator.currentBlock.getInstructions();
boolean asyncInvocation = false;
for (int j = 0; j < instructions.size(); ++j) { for (int j = 0; j < instructions.size(); ++j) {
Instruction insn = generator.currentBlock.getInstructions().get(j); Instruction insn = generator.currentBlock.getInstructions().get(j);
if (insn.getLocation() != null && lastLocation != insn.getLocation()) { if (insn.getLocation() != null && lastLocation != insn.getLocation()) {
@ -308,26 +311,48 @@ public class Decompiler {
} }
if (targetBlocks[node] >= 0 && j == instructions.size() - 1) { if (targetBlocks[node] >= 0 && j == instructions.size() - 1) {
generator.asyncTarget = targetBlocks[node]; generator.asyncTarget = targetBlocks[node];
asyncInvocation = true;
} }
insn.acceptVisitor(generator); insn.acceptVisitor(generator);
} }
boolean hasAsyncCatch = false;
for (TryCatchBlock tryCatch : generator.currentBlock.getTryCatchBlocks()) { for (TryCatchBlock tryCatch : generator.currentBlock.getTryCatchBlocks()) {
TryCatchStatement tryCatchStmt = new TryCatchStatement(); if (asyncInvocation) {
tryCatchStmt.setExceptionType(tryCatch.getExceptionType()); TryCatchStatement tryCatchStmt = new TryCatchStatement();
tryCatchStmt.setExceptionVariable(tryCatch.getExceptionVariable().getIndex()); tryCatchStmt.setExceptionType(tryCatch.getExceptionType());
tryCatchStmt.getProtectedBody().addAll(generator.statements); tryCatchStmt.setExceptionVariable(tryCatch.getExceptionVariable().getIndex());
generator.statements.clear(); tryCatchStmt.getProtectedBody().addAll(generator.statements);
generator.statements.add(tryCatchStmt); generator.statements.clear();
Statement handlerStmt = generator.generateJumpStatement(tryCatch.getHandler()); generator.statements.add(tryCatchStmt);
if (handlerStmt != null) { Statement handlerStmt = generator.generateJumpStatement(tryCatch.getHandler());
tryCatchStmt.getHandler().add(handlerStmt); if (handlerStmt != null) {
tryCatchStmt.getHandler().add(handlerStmt);
}
} else {
AsyncMethodCatch asyncCatch = new AsyncMethodCatch();
asyncCatch.setExceptionType(tryCatch.getExceptionType());
asyncCatch.setExceptionVariable(tryCatch.getExceptionVariable().getIndex());
Statement handlerStmt = generator.generateJumpStatement(tryCatch.getHandler());
if (handlerStmt != null) {
asyncCatch.getHandler().add(handlerStmt);
}
result.getCatches().add(asyncCatch);
hasAsyncCatch = true;
} }
} }
if (hasAsyncCatch) {
TryCatchStatement guardTryCatch = new TryCatchStatement();
guardTryCatch.setAsync(true);
guardTryCatch.getProtectedBody().addAll(generator.statements);
generator.statements.clear();
generator.statements.add(guardTryCatch);
}
block.body.addAll(generator.statements); block.body.addAll(generator.statements);
} }
} }
SequentialStatement result = new SequentialStatement(); SequentialStatement resultBody = new SequentialStatement();
result.getSequence().addAll(rootStmt.getBody()); resultBody.getSequence().addAll(rootStmt.getBody());
result.setStatement(resultBody);
return result; return result;
} }

View File

@ -939,11 +939,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
if (statement.getLocation() != null) { if (statement.getLocation() != null) {
pushLocation(statement.getLocation()); pushLocation(statement.getLocation());
} }
if (!async) { writer.append("$rt_throw(");
writer.append("$rt_throw(");
} else {
writer.append("return $throw(");
}
prevCallSite = debugEmitter.emitCallSite(); prevCallSite = debugEmitter.emitCallSite();
statement.getException().acceptVisitor(this); statement.getException().acceptVisitor(this);
writer.append(");").softNewLine(); writer.append(");").softNewLine();

View File

@ -0,0 +1,49 @@
/*
* Copyright 2015 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.javascript.ast;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Alexey Andreev
*/
public class AsyncMethodCatch {
private List<Statement> handler = new ArrayList<>();
private String exceptionType;
private Integer exceptionVariable;
public List<Statement> getHandler() {
return handler;
}
public String getExceptionType() {
return exceptionType;
}
public void setExceptionType(String exceptionType) {
this.exceptionType = exceptionType;
}
public Integer getExceptionVariable() {
return exceptionVariable;
}
public void setExceptionVariable(Integer exceptionVariable) {
this.exceptionVariable = exceptionVariable;
}
}

View File

@ -15,6 +15,9 @@
*/ */
package org.teavm.javascript.ast; package org.teavm.javascript.ast;
import java.util.ArrayList;
import java.util.List;
/** /**
* *
* @author Alexey Andreev * @author Alexey Andreev
@ -22,6 +25,7 @@ package org.teavm.javascript.ast;
public class AsyncMethodPart { public class AsyncMethodPart {
private Statement statement; private Statement statement;
private Integer inputVariable; private Integer inputVariable;
private List<AsyncMethodCatch> catches = new ArrayList<>();
public Statement getStatement() { public Statement getStatement() {
return statement; return statement;
@ -38,4 +42,8 @@ public class AsyncMethodPart {
public void setInputVariable(Integer inputVariable) { public void setInputVariable(Integer inputVariable) {
this.inputVariable = inputVariable; this.inputVariable = inputVariable;
} }
public List<AsyncMethodCatch> getCatches() {
return catches;
}
} }

View File

@ -27,6 +27,7 @@ public class TryCatchStatement extends Statement {
private List<Statement> handler = new ArrayList<>(); private List<Statement> handler = new ArrayList<>();
private String exceptionType; private String exceptionType;
private Integer exceptionVariable; private Integer exceptionVariable;
private boolean async;
public List<Statement> getProtectedBody() { public List<Statement> getProtectedBody() {
return protectedBody; return protectedBody;
@ -52,6 +53,14 @@ public class TryCatchStatement extends Statement {
this.exceptionVariable = exceptionVariable; this.exceptionVariable = exceptionVariable;
} }
public boolean isAsync() {
return async;
}
public void setAsync(boolean async) {
this.async = async;
}
@Override @Override
public void acceptVisitor(StatementVisitor visitor) { public void acceptVisitor(StatementVisitor visitor) {
visitor.visit(this); visitor.visit(this);

View File

@ -69,6 +69,14 @@ public class AsyncProgramSplitter {
targetBlock.getInstructions().addAll(ProgramUtils.copyInstructions(sourceBlock, targetBlock.getInstructions().addAll(ProgramUtils.copyInstructions(sourceBlock,
last, i + 1, targetBlock.getProgram())); last, i + 1, targetBlock.getProgram()));
ProgramUtils.copyTryCatches(sourceBlock, targetBlock.getProgram()); ProgramUtils.copyTryCatches(sourceBlock, targetBlock.getProgram());
for (TryCatchBlock tryCatch : targetBlock.getTryCatchBlocks()) {
if (tryCatch.getHandler() != null) {
Step next = new Step();
next.source = tryCatch.getHandler().getIndex();
next.targetPart = step.targetPart;
queue.add(next);
}
}
last = i + 1; last = i + 1;
// If this instruction already separates program, end with current block and refer to the // If this instruction already separates program, end with current block and refer to the