Fixes bugs in try/catch

This commit is contained in:
konsoletyper 2014-02-26 17:54:25 +04:00
parent ea3e6adb64
commit a4f477329d
6 changed files with 81 additions and 6 deletions

View File

@ -0,0 +1,40 @@
/*
* Copyright 2014 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.classlib.java.lang;
/**
*
* @author Alexey Andreev
*/
public class TSecurityException extends TRuntimeException {
private static final long serialVersionUID = 720572643357872552L;
public TSecurityException() {
super();
}
public TSecurityException(TString message, TThrowable cause) {
super(message, cause);
}
public TSecurityException(TString message) {
super(message);
}
public TSecurityException(TThrowable cause) {
super(cause);
}
}

View File

@ -60,6 +60,16 @@ class DependencyGraphBuilder {
nodes[incoming.getValue().getIndex()].connect(nodes[phi.getReceiver().getIndex()]);
}
}
for (final TryCatchBlockReader tryCatch : block.readTryCatchBlocks()) {
useRunners.add(new Runnable() {
@Override
public void run() {
if (tryCatch.getExceptionType() != null) {
dependencyChecker.initClass(tryCatch.getExceptionType(), callerStack);
}
}
});
}
}
dep.setUseRunner(new MultipleRunner(useRunners));
}

View File

@ -213,6 +213,9 @@ public class Decompiler {
if (node >= 0) {
generator.currentBlock = program.basicBlockAt(node);
int tmp = indexer.nodeAt(next);
if (tmp < 0) {
throw new IllegalArgumentException();
}
generator.nextBlock = next < indexer.size() ? program.basicBlockAt(tmp) : null;
generator.statements.clear();
for (Instruction insn : generator.currentBlock.getInstructions()) {

View File

@ -1268,8 +1268,12 @@ public class Renderer implements ExprVisitor, StatementVisitor {
.ws().append("{").indent().softNewLine();
writer.append("$je").ws().append("=").ws().append("$e.$javaException;").softNewLine();
for (TryCatchStatement catchClause : sequence) {
writer.append("if").ws().append("($je && $je instanceof ").appendClass(catchClause.getExceptionType())
.append(")").ws().append("{").indent().softNewLine();
writer.append("if").ws().append("($je");
if (catchClause.getExceptionType() != null) {
writer.ws().append("&&").ws().append("$je instanceof ")
.appendClass(catchClause.getExceptionType());
}
writer.append(")").ws().append("{").indent().softNewLine();
if (statement.getExceptionVariable() != catchClause.getExceptionVariable()) {
if (catchClause.getExceptionVariable() != null) {
writer.append(variableName(catchClause.getExceptionVariable())).ws().append("=").ws()

View File

@ -25,6 +25,24 @@ import org.teavm.model.instructions.*;
* @author Alexey Andreev
*/
public class ProgramUtils {
public static Graph buildControlFlowGraphWithoutTryCatch(Program program) {
GraphBuilder graphBuilder = new GraphBuilder(program.basicBlockCount());
InstructionTransitionExtractor transitionExtractor = new InstructionTransitionExtractor();
for (int i = 0; i < program.basicBlockCount(); ++i) {
BasicBlock block = program.basicBlockAt(i);
Instruction insn = block.getLastInstruction();
if (insn != null) {
insn.acceptVisitor(transitionExtractor);
if (transitionExtractor.getTargets() != null) {
for (BasicBlock successor : transitionExtractor.getTargets()) {
graphBuilder.addEdge(i, successor.getIndex());
}
}
}
}
return graphBuilder.build();
}
public static Graph buildControlFlowGraph(Program program) {
GraphBuilder graphBuilder = new GraphBuilder(program.basicBlockCount());
InstructionTransitionExtractor transitionExtractor = new InstructionTransitionExtractor();

View File

@ -32,7 +32,6 @@ public class SSATransformer {
private Program program;
private Graph cfg;
private DominatorTree domTree;
private Graph domGraph;
private int[][] domFrontiers;
private Variable[] variableMap;
private BasicBlock currentBlock;
@ -46,7 +45,7 @@ public class SSATransformer {
}
this.program = program;
this.arguments = arguments;
cfg = ProgramUtils.buildControlFlowGraph(program);
cfg = ProgramUtils.buildControlFlowGraphWithoutTryCatch(program);
domTree = GraphUtils.buildDominatorTree(cfg);
domFrontiers = new int[cfg.size()][];
variableMap = new Variable[program.variableCount()];
@ -103,11 +102,12 @@ public class SSATransformer {
}
private void renameVariables() {
domGraph = GraphUtils.buildDominatorGraph(domTree, program.basicBlockCount());
DominatorTree domTree = GraphUtils.buildDominatorTree(ProgramUtils.buildControlFlowGraph(program));
Graph domGraph = GraphUtils.buildDominatorGraph(domTree, program.basicBlockCount());
Task[] stack = new Task[cfg.size() * 2];
int head = 0;
for (int i = 0; i < program.basicBlockCount(); ++i) {
if (domTree.immediateDominatorOf(i) < 0) {
if (domGraph.incomingEdgesCount(i) == 0) {
Task task = new Task();
task.block = program.basicBlockAt(i);
task.variables = Arrays.copyOf(variableMap, variableMap.length);