Change how exceptions handlers are represented

This commit is contained in:
Alexey Andreev 2016-07-16 19:03:54 +03:00 committed by Alexey Andreev
parent 4f8affcf6f
commit a26eed7c51
30 changed files with 376 additions and 467 deletions

View File

@ -50,6 +50,8 @@ public class ProgramIO {
} }
for (int i = 0; i < program.basicBlockCount(); ++i) { for (int i = 0; i < program.basicBlockCount(); ++i) {
BasicBlock basicBlock = program.basicBlockAt(i); BasicBlock basicBlock = program.basicBlockAt(i);
data.writeShort(basicBlock.getExceptionVariable() != null
? basicBlock.getExceptionVariable().getIndex() : -1);
data.writeShort(basicBlock.getPhis().size()); data.writeShort(basicBlock.getPhis().size());
data.writeShort(basicBlock.getTryCatchBlocks().size()); data.writeShort(basicBlock.getTryCatchBlocks().size());
for (Phi phi : basicBlock.getPhis()) { for (Phi phi : basicBlock.getPhis()) {
@ -63,17 +65,14 @@ public class ProgramIO {
for (TryCatchBlock tryCatch : basicBlock.getTryCatchBlocks()) { for (TryCatchBlock tryCatch : basicBlock.getTryCatchBlocks()) {
data.writeInt(tryCatch.getExceptionType() != null ? symbolTable.lookup( data.writeInt(tryCatch.getExceptionType() != null ? symbolTable.lookup(
tryCatch.getExceptionType()) : -1); tryCatch.getExceptionType()) : -1);
data.writeShort(tryCatch.getExceptionVariable() != null
? tryCatch.getExceptionVariable().getIndex() : -1);
data.writeShort(tryCatch.getHandler().getIndex()); data.writeShort(tryCatch.getHandler().getIndex());
} data.writeShort(tryCatch.getTryCatchJoints().size());
data.writeShort(basicBlock.getTryCatchJoints().size()); for (TryCatchJoint joint : tryCatch.getTryCatchJoints()) {
for (TryCatchJoint joint : basicBlock.getTryCatchJoints()) { data.writeShort(joint.getReceiver().getIndex());
data.writeShort(joint.getSource().getIndex()); data.writeShort(joint.getSourceVariables().size());
data.writeShort(joint.getReceiver().getIndex()); for (Variable sourceVar : joint.getSourceVariables()) {
data.writeShort(joint.getSourceVariables().size()); data.writeShort(sourceVar.getIndex());
for (Variable sourceVar : joint.getSourceVariables()) { }
data.writeShort(sourceVar.getIndex());
} }
} }
InstructionLocation location = null; InstructionLocation location = null;
@ -117,6 +116,12 @@ public class ProgramIO {
} }
for (int i = 0; i < basicBlockCount; ++i) { for (int i = 0; i < basicBlockCount; ++i) {
BasicBlock block = program.basicBlockAt(i); BasicBlock block = program.basicBlockAt(i);
short varIndex = data.readShort();
if (varIndex >= 0) {
block.setExceptionVariable(program.variableAt(varIndex));
}
int phiCount = data.readShort(); int phiCount = data.readShort();
int tryCatchCount = data.readShort(); int tryCatchCount = data.readShort();
for (int j = 0; j < phiCount; ++j) { for (int j = 0; j < phiCount; ++j) {
@ -137,24 +142,20 @@ public class ProgramIO {
if (typeIndex >= 0) { if (typeIndex >= 0) {
tryCatch.setExceptionType(symbolTable.at(typeIndex)); tryCatch.setExceptionType(symbolTable.at(typeIndex));
} }
short varIndex = data.readShort();
if (varIndex >= 0) {
tryCatch.setExceptionVariable(program.variableAt(varIndex));
}
tryCatch.setHandler(program.basicBlockAt(data.readShort())); tryCatch.setHandler(program.basicBlockAt(data.readShort()));
block.getTryCatchBlocks().add(tryCatch);
}
int jointCount = data.readShort(); int jointCount = data.readShort();
for (int j = 0; j < jointCount; ++j) { for (int k = 0; k < jointCount; ++k) {
TryCatchJoint joint = new TryCatchJoint(); TryCatchJoint joint = new TryCatchJoint();
joint.setSource(program.basicBlockAt(data.readShort())); joint.setReceiver(program.variableAt(data.readShort()));
joint.setReceiver(program.variableAt(data.readShort())); int jointSourceCount = data.readShort();
int jointSourceCount = data.readShort(); for (int m = 0; m < jointSourceCount; ++m) {
for (int k = 0; k < jointSourceCount; ++k) { joint.getSourceVariables().add(program.variableAt(data.readShort()));
joint.getSourceVariables().add(program.variableAt(data.readShort())); }
tryCatch.getTryCatchJoints().add(joint);
} }
block.getTryCatchJoints().add(joint);
block.getTryCatchBlocks().add(tryCatch);
} }
InstructionLocation location = null; InstructionLocation location = null;

View File

@ -64,9 +64,11 @@ public class DataFlowGraphBuilder implements InstructionReader {
builder.addEdge(from, to); builder.addEdge(from, to);
} }
} }
for (TryCatchJointReader joint : block.readTryCatchJoints()) { for (TryCatchBlockReader tryCatch : block.readTryCatchBlocks()) {
for (VariableReader sourceVar : joint.readSourceVariables()) { for (TryCatchJointReader joint : tryCatch.readTryCatchJoints()) {
builder.addEdge(sourceVar.getIndex(), joint.getReceiver().getIndex()); for (VariableReader sourceVar : joint.readSourceVariables()) {
builder.addEdge(sourceVar.getIndex(), joint.getReceiver().getIndex());
}
} }
} }
block.readAllInstructions(this); block.readAllInstructions(this);

View File

@ -137,18 +137,7 @@ class DependencyGraphBuilder {
BasicBlockReader block = program.basicBlockAt(i); BasicBlockReader block = program.basicBlockAt(i);
currentExceptionConsumer = createExceptionConsumer(dep, block); currentExceptionConsumer = createExceptionConsumer(dep, block);
block.readAllInstructions(reader); block.readAllInstructions(reader);
for (TryCatchJointReader joint : block.readTryCatchJoints()) {
DependencyNode receiverNode = nodes[joint.getReceiver().getIndex()];
if (receiverNode == null) {
continue;
}
for (VariableReader source : joint.readSourceVariables()) {
DependencyNode sourceNode = nodes[source.getIndex()];
if (sourceNode != null) {
sourceNode.connect(receiverNode);
}
}
}
for (PhiReader phi : block.readPhis()) { for (PhiReader phi : block.readPhis()) {
DependencyNode receiverNode = nodes[phi.getReceiver().getIndex()]; DependencyNode receiverNode = nodes[phi.getReceiver().getIndex()];
for (IncomingReader incoming : phi.readIncomings()) { for (IncomingReader incoming : phi.readIncomings()) {
@ -158,10 +147,24 @@ class DependencyGraphBuilder {
} }
} }
} }
for (TryCatchBlockReader tryCatch : block.readTryCatchBlocks()) { for (TryCatchBlockReader tryCatch : block.readTryCatchBlocks()) {
if (tryCatch.getExceptionType() != null) { if (tryCatch.getExceptionType() != null) {
dependencyChecker.linkClass(tryCatch.getExceptionType(), new CallLocation(caller.getMethod())); dependencyChecker.linkClass(tryCatch.getExceptionType(), new CallLocation(caller.getMethod()));
} }
for (TryCatchJointReader joint : tryCatch.readTryCatchJoints()) {
DependencyNode receiverNode = nodes[joint.getReceiver().getIndex()];
if (receiverNode == null) {
continue;
}
for (VariableReader source : joint.readSourceVariables()) {
DependencyNode sourceNode = nodes[source.getIndex()];
if (sourceNode != null) {
sourceNode.connect(receiverNode);
}
}
}
} }
} }
@ -286,7 +289,7 @@ class DependencyGraphBuilder {
if (tryCatch.getExceptionType() != null) { if (tryCatch.getExceptionType() != null) {
exceptions[i] = dependencyChecker.getClassSource().get(tryCatch.getExceptionType()); exceptions[i] = dependencyChecker.getClassSource().get(tryCatch.getExceptionType());
} }
vars[i] = methodDep.getVariable(tryCatch.getExceptionVariable().getIndex()); vars[i] = methodDep.getVariable(tryCatch.getHandler().getExceptionVariable().getIndex());
} }
return new ExceptionConsumer(dependencyChecker, exceptions, vars, methodDep); return new ExceptionConsumer(dependencyChecker, exceptions, vars, methodDep);
} }

View File

@ -541,8 +541,8 @@ public class Decompiler {
bookmark.offset = bookmark.block.body.size(); bookmark.offset = bookmark.block.body.size();
bookmark.exceptionHandler = tryCatch.getHandler().getIndex(); bookmark.exceptionHandler = tryCatch.getHandler().getIndex();
bookmark.exceptionType = tryCatch.getExceptionType(); bookmark.exceptionType = tryCatch.getExceptionType();
bookmark.exceptionVariable = tryCatch.getExceptionVariable() != null bookmark.exceptionVariable = tryCatch.getHandler().getExceptionVariable() != null
? tryCatch.getExceptionVariable().getIndex() : null; ? tryCatch.getHandler().getExceptionVariable().getIndex() : null;
bookmark.block.tryCatches.add(bookmark); bookmark.block.tryCatches.add(bookmark);
tryCatchBookmarks.add(bookmark); tryCatchBookmarks.add(bookmark);
} }

View File

@ -53,6 +53,12 @@ class ReadWriteStatsBuilder {
while (!stack.isEmpty()) { while (!stack.isEmpty()) {
int node = stack.pop(); int node = stack.pop();
BasicBlock block = program.basicBlockAt(node); BasicBlock block = program.basicBlockAt(node);
if (block.getExceptionVariable() != null) {
writes[block.getExceptionVariable().getIndex()]++;
reads[block.getExceptionVariable().getIndex()]++;
}
for (Instruction insn : block.getInstructions()) { for (Instruction insn : block.getInstructions()) {
insn.acceptVisitor(defExtractor); insn.acceptVisitor(defExtractor);
insn.acceptVisitor(useExtractor); insn.acceptVisitor(useExtractor);
@ -64,12 +70,6 @@ class ReadWriteStatsBuilder {
} }
} }
for (TryCatchJoint joint : block.getTryCatchJoints()) {
writes[joint.getReceiver().getIndex()] += joint.getSourceVariables().size();
for (Variable var : joint.getSourceVariables()) {
reads[var.getIndex()]++;
}
}
for (Phi phi : block.getPhis()) { for (Phi phi : block.getPhis()) {
writes[phi.getReceiver().getIndex()] += phi.getIncomings().size(); writes[phi.getReceiver().getIndex()] += phi.getIncomings().size();
for (Incoming incoming : phi.getIncomings()) { for (Incoming incoming : phi.getIncomings()) {
@ -79,8 +79,12 @@ class ReadWriteStatsBuilder {
} }
} }
for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) { for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
writes[tryCatch.getExceptionVariable().getIndex()]++; for (TryCatchJoint joint : tryCatch.getTryCatchJoints()) {
reads[tryCatch.getExceptionVariable().getIndex()]++; writes[joint.getReceiver().getIndex()] += joint.getSourceVariables().size();
for (Variable var : joint.getSourceVariables()) {
reads[var.getIndex()]++;
}
}
} }
for (int succ : dom.outgoingEdges(node)) { for (int succ : dom.outgoingEdges(node)) {

View File

@ -24,8 +24,7 @@ public class BasicBlock implements BasicBlockReader {
private List<Phi> phis = new ArrayList<>(); private List<Phi> phis = new ArrayList<>();
private List<Instruction> instructions = new ArrayList<>(); private List<Instruction> instructions = new ArrayList<>();
private List<TryCatchBlock> tryCatchBlocks = new ArrayList<>(); private List<TryCatchBlock> tryCatchBlocks = new ArrayList<>();
private List<TryCatchJoint> joints = new ArrayList<>(); private Variable exceptionVariable;
private List<TryCatchJointReader> immutableJoints;
BasicBlock(Program program, int index) { BasicBlock(Program program, int index) {
this.program = program; this.program = program;
@ -152,53 +151,6 @@ public class BasicBlock implements BasicBlockReader {
} }
}; };
private List<TryCatchJoint> safeJoints = new AbstractList<TryCatchJoint>() {
@Override
public TryCatchJoint get(int index) {
return joints.get(index);
}
@Override
public int size() {
return joints.size();
}
@Override
public void add(int index, TryCatchJoint e) {
if (e.getBlock() != null) {
throw new IllegalArgumentException("This joint is already in some basic block");
}
e.block = BasicBlock.this;
joints.add(index, e);
}
@Override
public TryCatchJoint set(int index, TryCatchJoint element) {
if (element.block != null) {
throw new IllegalArgumentException("This phi is already in some basic block");
}
TryCatchJoint oldJoint = joints.get(index);
oldJoint.block = null;
element.block = BasicBlock.this;
return joints.set(index, element);
}
@Override
public TryCatchJoint remove(int index) {
TryCatchJoint joint = joints.remove(index);
joint.block = null;
return joint;
}
@Override
public void clear() {
for (TryCatchJoint joint : joints) {
joint.block = null;
}
joints.clear();
}
};
public List<Phi> getPhis() { public List<Phi> getPhis() {
return safePhis; return safePhis;
} }
@ -244,12 +196,6 @@ public class BasicBlock implements BasicBlockReader {
} }
} }
} }
for (int i = 0; i < joints.size(); ++i) {
TryCatchJoint joint = joints.get(i);
if (joint.getSource() == predecessor) {
joints.remove(i--);
}
}
} }
private List<TryCatchBlock> immutableTryCatchBlocks = Collections.unmodifiableList(tryCatchBlocks); private List<TryCatchBlock> immutableTryCatchBlocks = Collections.unmodifiableList(tryCatchBlocks);
@ -303,15 +249,12 @@ public class BasicBlock implements BasicBlockReader {
return safeTryCatchBlocks; return safeTryCatchBlocks;
} }
public List<TryCatchJoint> getTryCatchJoints() { @Override
return safeJoints; public Variable getExceptionVariable() {
return exceptionVariable;
} }
@Override public void setExceptionVariable(Variable exceptionVariable) {
public List<TryCatchJointReader> readTryCatchJoints() { this.exceptionVariable = exceptionVariable;
if (immutableJoints == null) {
immutableJoints = Collections.unmodifiableList(safeJoints);
}
return immutableJoints;
} }
} }

View File

@ -33,5 +33,5 @@ public interface BasicBlockReader {
List<? extends TryCatchBlockReader> readTryCatchBlocks(); List<? extends TryCatchBlockReader> readTryCatchBlocks();
List<TryCatchJointReader> readTryCatchJoints(); VariableReader getExceptionVariable();
} }

View File

@ -15,11 +15,64 @@
*/ */
package org.teavm.model; package org.teavm.model;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TryCatchBlock implements TryCatchBlockReader { public class TryCatchBlock implements TryCatchBlockReader {
BasicBlock protectedBlock; BasicBlock protectedBlock;
private BasicBlock handler; private BasicBlock handler;
private String exceptionType; private String exceptionType;
private Variable exceptionVariable; private List<TryCatchJoint> joints = new ArrayList<>();
private List<TryCatchJointReader> immutableJoints;
private List<TryCatchJoint> safeJoints = new AbstractList<TryCatchJoint>() {
@Override
public TryCatchJoint get(int index) {
return joints.get(index);
}
@Override
public int size() {
return joints.size();
}
@Override
public void add(int index, TryCatchJoint e) {
if (e.getBlock() != null) {
throw new IllegalArgumentException("This joint is already in some basic block");
}
e.block = TryCatchBlock.this;
joints.add(index, e);
}
@Override
public TryCatchJoint set(int index, TryCatchJoint element) {
if (element.block != null) {
throw new IllegalArgumentException("This phi is already in some basic block");
}
TryCatchJoint oldJoint = joints.get(index);
oldJoint.block = null;
element.block = TryCatchBlock.this;
return joints.set(index, element);
}
@Override
public TryCatchJoint remove(int index) {
TryCatchJoint joint = joints.remove(index);
joint.block = null;
return joint;
}
@Override
public void clear() {
for (TryCatchJoint joint : joints) {
joint.block = null;
}
joints.clear();
}
};
@Override @Override
public BasicBlock getHandler() { public BasicBlock getHandler() {
@ -39,17 +92,20 @@ public class TryCatchBlock implements TryCatchBlockReader {
this.exceptionType = exceptionType; this.exceptionType = exceptionType;
} }
@Override
public Variable getExceptionVariable() {
return exceptionVariable;
}
public void setExceptionVariable(Variable exceptionVariable) {
this.exceptionVariable = exceptionVariable;
}
@Override @Override
public BasicBlock getProtectedBlock() { public BasicBlock getProtectedBlock() {
return protectedBlock; return protectedBlock;
} }
public List<TryCatchJoint> getTryCatchJoints() {
return safeJoints;
}
@Override
public List<TryCatchJointReader> readTryCatchJoints() {
if (immutableJoints == null) {
immutableJoints = Collections.unmodifiableList(safeJoints);
}
return immutableJoints;
}
} }

View File

@ -15,6 +15,8 @@
*/ */
package org.teavm.model; package org.teavm.model;
import java.util.List;
public interface TryCatchBlockReader { public interface TryCatchBlockReader {
BasicBlockReader getProtectedBlock(); BasicBlockReader getProtectedBlock();
@ -22,5 +24,5 @@ public interface TryCatchBlockReader {
String getExceptionType(); String getExceptionType();
VariableReader getExceptionVariable(); List<TryCatchJointReader> readTryCatchJoints();
} }

View File

@ -20,11 +20,10 @@ import java.util.Collections;
import java.util.List; import java.util.List;
public class TryCatchJoint implements TryCatchJointReader { public class TryCatchJoint implements TryCatchJointReader {
private BasicBlock source;
private List<Variable> sourceVariables = new ArrayList<>(); private List<Variable> sourceVariables = new ArrayList<>();
private List<VariableReader> readonlySourceVariables; private List<VariableReader> readonlySourceVariables;
private Variable receiver; private Variable receiver;
BasicBlock block; TryCatchBlock block;
@Override @Override
public List<VariableReader> readSourceVariables() { public List<VariableReader> readSourceVariables() {
@ -47,16 +46,7 @@ public class TryCatchJoint implements TryCatchJointReader {
this.receiver = receiver; this.receiver = receiver;
} }
@Override public TryCatchBlock getBlock() {
public BasicBlock getSource() {
return source;
}
public void setSource(BasicBlock source) {
this.source = source;
}
public BasicBlock getBlock() {
return block; return block;
} }
} }

View File

@ -22,7 +22,5 @@ public interface TryCatchJointReader {
VariableReader getReceiver(); VariableReader getReceiver();
BasicBlockReader getSource(); TryCatchBlockReader getBlock();
BasicBlockReader getBlock();
} }

View File

@ -43,9 +43,7 @@ public class BasicBlockMapper implements InstructionVisitor {
return; return;
} }
lastInsn.acceptVisitor(this); lastInsn.acceptVisitor(this);
for (TryCatchJoint joint : block.getTryCatchJoints()) {
joint.setSource(map(joint.getSource()));
}
for (Phi phi : block.getPhis()) { for (Phi phi : block.getPhis()) {
for (Incoming incoming : phi.getIncomings()) { for (Incoming incoming : phi.getIncomings()) {
incoming.setSource(map(incoming.getSource())); incoming.setSource(map(incoming.getSource()));

View File

@ -34,10 +34,13 @@ public class InstructionVariableMapper implements InstructionVisitor {
} }
public void apply(BasicBlock block) { public void apply(BasicBlock block) {
if (block.getExceptionVariable() != null) {
block.setExceptionVariable(map(block.getExceptionVariable()));
}
applyToInstructions(block); applyToInstructions(block);
applyToPhis(block); applyToPhis(block);
applyToTryCatchBlocks(block); applyToTryCatchBlocks(block);
applyToTryCatchJoints(block);
} }
public void applyToInstructions(BasicBlock block) { public void applyToInstructions(BasicBlock block) {
@ -57,18 +60,12 @@ public class InstructionVariableMapper implements InstructionVisitor {
public void applyToTryCatchBlocks(BasicBlock block) { public void applyToTryCatchBlocks(BasicBlock block) {
for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) { for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
if (tryCatch.getExceptionVariable() != null) { for (TryCatchJoint joint : tryCatch.getTryCatchJoints()) {
tryCatch.setExceptionVariable(map(tryCatch.getExceptionVariable())); joint.setReceiver(map(joint.getReceiver()));
} for (int i = 0; i < joint.getSourceVariables().size(); ++i) {
} Variable var = joint.getSourceVariables().get(i);
} joint.getSourceVariables().set(i, map(var));
}
public void applyToTryCatchJoints(BasicBlock block) {
for (TryCatchJoint joint : block.getTryCatchJoints()) {
joint.setReceiver(map(joint.getReceiver()));
for (int i = 0; i < joint.getSourceVariables().size(); ++i) {
Variable var = joint.getSourceVariables().get(i);
joint.getSourceVariables().set(i, map(var));
} }
} }
} }

View File

@ -29,11 +29,11 @@ class InterferenceGraphBuilder {
DefinitionExtractor defExtractor = new DefinitionExtractor(); DefinitionExtractor defExtractor = new DefinitionExtractor();
InstructionTransitionExtractor succExtractor = new InstructionTransitionExtractor(); InstructionTransitionExtractor succExtractor = new InstructionTransitionExtractor();
List<List<Incoming>> outgoings = ProgramUtils.getPhiOutputs(program); List<List<Incoming>> outgoings = ProgramUtils.getPhiOutputs(program);
List<List<TryCatchJoint>> outputJoints = ProgramUtils.getOutputJoints(program);
Set<MutableGraphNode> live = new HashSet<>(128); Set<MutableGraphNode> live = new HashSet<>(128);
for (int i = 0; i < program.basicBlockCount(); ++i) { for (int i = 0; i < program.basicBlockCount(); ++i) {
BasicBlock block = program.basicBlockAt(i); BasicBlock block = program.basicBlockAt(i);
block.getLastInstruction().acceptVisitor(succExtractor); block.getLastInstruction().acceptVisitor(succExtractor);
BitSet liveOut = new BitSet(program.variableCount()); BitSet liveOut = new BitSet(program.variableCount());
for (BasicBlock succ : succExtractor.getTargets()) { for (BasicBlock succ : succExtractor.getTargets()) {
liveOut.or(liveness.liveIn(succ.getIndex())); liveOut.or(liveness.liveIn(succ.getIndex()));
@ -47,24 +47,20 @@ class InterferenceGraphBuilder {
live.add(nodes.get(j)); live.add(nodes.get(j));
} }
} }
for (TryCatchJoint joint : outputJoints.get(i)) {
for (Variable outputVar : joint.getSourceVariables()) {
live.add(nodes.get(outputVar.getIndex()));
}
}
for (Incoming outgoing : outgoings.get(i)) { for (Incoming outgoing : outgoings.get(i)) {
live.add(nodes.get(outgoing.getValue().getIndex())); live.add(nodes.get(outgoing.getValue().getIndex()));
} }
for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) { for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
if (tryCatch.getExceptionVariable() != null) { for (TryCatchJoint joint : tryCatch.getTryCatchJoints()) {
nodes.get(tryCatch.getExceptionVariable().getIndex()).connectAll(live); for (Variable sourceVar : joint.getSourceVariables()) {
} live.add(nodes.get(sourceVar.getIndex()));
} }
for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) { live.remove(nodes.get(joint.getReceiver().getIndex()));
if (tryCatch.getExceptionVariable() != null) {
live.remove(nodes.get(tryCatch.getExceptionVariable().getIndex()));
} }
} }
for (int j = block.getInstructions().size() - 1; j >= 0; --j) { for (int j = block.getInstructions().size() - 1; j >= 0; --j) {
Instruction insn = block.getInstructions().get(j); Instruction insn = block.getInstructions().get(j);
insn.acceptVisitor(useExtractor); insn.acceptVisitor(useExtractor);
@ -79,11 +75,16 @@ class InterferenceGraphBuilder {
live.add(nodes.get(var.getIndex())); live.add(nodes.get(var.getIndex()));
} }
} }
if (block.getExceptionVariable() != null) {
nodes.get(block.getExceptionVariable().getIndex()).connectAll(live);
live.remove(nodes.get(block.getExceptionVariable().getIndex()));
}
if (block.getIndex() == 0) { if (block.getIndex() == 0) {
for (int j = 0; j <= paramCount; ++j) { for (int j = 0; j <= paramCount; ++j) {
nodes.get(j).connectAll(live); nodes.get(j).connectAll(live);
} }
} }
BitSet liveIn = liveness.liveIn(i); BitSet liveIn = liveness.liveIn(i);
live.clear(); live.clear();
for (int j = 0; j < liveOut.length(); ++j) { for (int j = 0; j < liveOut.length(); ++j) {
@ -92,16 +93,10 @@ class InterferenceGraphBuilder {
} }
} }
for (TryCatchJoint joint : block.getTryCatchJoints()) {
live.add(nodes.get(joint.getReceiver().getIndex()));
}
for (Phi phi : block.getPhis()) { for (Phi phi : block.getPhis()) {
live.add(nodes.get(phi.getReceiver().getIndex())); live.add(nodes.get(phi.getReceiver().getIndex()));
} }
for (TryCatchJoint joint : block.getTryCatchJoints()) {
nodes.get(joint.getReceiver().getIndex()).connectAll(live);
}
for (Phi phi : block.getPhis()) { for (Phi phi : block.getPhis()) {
nodes.get(phi.getReceiver().getIndex()).connectAll(live); nodes.get(phi.getReceiver().getIndex()).connectAll(live);
} }

View File

@ -48,12 +48,10 @@ public class ListingBuilder {
continue; continue;
} }
for (TryCatchJointReader joint : block.readTryCatchJoints()) { if (block.getExceptionVariable() != null) {
sb.append(" @").append(joint.getReceiver().getIndex()).append(" := e-phi("); sb.append("@").append(block.getExceptionVariable().getIndex()).append(" = exception");
sb.append(joint.readSourceVariables().stream().map(sourceVar -> "@" + sourceVar.getIndex())
.collect(Collectors.joining(", ")));
sb.append(") from $").append(joint.getSource().getIndex()).append("\n");
} }
for (PhiReader phi : block.readPhis()) { for (PhiReader phi : block.readPhis()) {
sb.append(prefix).append(" "); sb.append(prefix).append(" ");
sb.append("@").append(phi.getReceiver().getIndex()).append(" := "); sb.append("@").append(phi.getReceiver().getIndex()).append(" := ");
@ -81,10 +79,15 @@ public class ListingBuilder {
sb.append(prefix).append(" ").append(insnSb).append("\n"); sb.append(prefix).append(" ").append(insnSb).append("\n");
} }
for (TryCatchBlockReader tryCatch : block.readTryCatchBlocks()) { for (TryCatchBlockReader tryCatch : block.readTryCatchBlocks()) {
sb.append(prefix).append(" catch ").append(tryCatch.getExceptionType()).append(" @") sb.append(prefix).append(" catch ").append(tryCatch.getExceptionType())
.append(tryCatch.getExceptionVariable().getIndex())
.append(" -> $").append(tryCatch.getHandler().getIndex()); .append(" -> $").append(tryCatch.getHandler().getIndex());
sb.append("\n"); sb.append("\n");
for (TryCatchJointReader joint : tryCatch.readTryCatchJoints()) {
sb.append(" @").append(joint.getReceiver().getIndex()).append(" := e-phi(");
sb.append(joint.readSourceVariables().stream().map(sourceVar -> "@" + sourceVar.getIndex())
.collect(Collectors.joining(", ")));
sb.append(")\n");
}
} }
} }
return sb.toString(); return sb.toString();

View File

@ -47,6 +47,11 @@ public class LivenessAnalyzer {
int[] definitions = new int[program.variableCount()]; int[] definitions = new int[program.variableCount()];
for (int i = 0; i < program.basicBlockCount(); ++i) { for (int i = 0; i < program.basicBlockCount(); ++i) {
BasicBlock block = program.basicBlockAt(i); BasicBlock block = program.basicBlockAt(i);
if (block.getExceptionVariable() != null) {
definitions[block.getExceptionVariable().getIndex()] = i;
}
for (Instruction insn : block.getInstructions()) { for (Instruction insn : block.getInstructions()) {
insn.acceptVisitor(usageExtractor); insn.acceptVisitor(usageExtractor);
IntSet usedVars = new IntOpenHashSet(); IntSet usedVars = new IntOpenHashSet();
@ -64,21 +69,19 @@ public class LivenessAnalyzer {
} }
} }
} }
for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) { for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
if (tryCatch.getExceptionVariable() != null) { for (TryCatchJoint joint : tryCatch.getTryCatchJoints()) {
definitions[tryCatch.getExceptionVariable().getIndex()] = i; definitions[joint.getReceiver().getIndex()] = i;
for (Variable sourceVar : joint.getSourceVariables()) {
Task task = new Task();
task.block = i;
task.var = sourceVar.getIndex();
stack.push(task);
}
} }
} }
for (TryCatchJoint joint : block.getTryCatchJoints()) {
definitions[joint.getReceiver().getIndex()] = i;
for (Variable sourceVar : joint.getSourceVariables()) {
Task task = new Task();
task.block = joint.getSource().getIndex();
task.var = sourceVar.getIndex();
stack.push(task);
}
}
for (Phi phi : block.getPhis()) { for (Phi phi : block.getPhis()) {
definitions[phi.getReceiver().getIndex()] = i; definitions[phi.getReceiver().getIndex()] = i;
for (Incoming incoming : phi.getIncomings()) { for (Incoming incoming : phi.getIncomings()) {

View File

@ -17,7 +17,6 @@ package org.teavm.model.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -81,7 +80,6 @@ public class PhiUpdater {
private int[][] phiIndexMap; private int[][] phiIndexMap;
private List<Map<BasicBlock, Map<Variable, TryCatchJoint>>> jointMap = new ArrayList<>(); private List<Map<BasicBlock, Map<Variable, TryCatchJoint>>> jointMap = new ArrayList<>();
private List<List<Phi>> synthesizedPhis = new ArrayList<>(); private List<List<Phi>> synthesizedPhis = new ArrayList<>();
private List<List<TryCatchJoint>> synthesizedJoints = new ArrayList<>();
private boolean[] usedDefinitions; private boolean[] usedDefinitions;
public void updatePhis(Program program, Variable[] arguments) { public void updatePhis(Program program, Variable[] arguments) {
@ -109,10 +107,8 @@ public class PhiUpdater {
domFrontiers = GraphUtils.findDominanceFrontiers(cfg, domTree); domFrontiers = GraphUtils.findDominanceFrontiers(cfg, domTree);
synthesizedPhis.clear(); synthesizedPhis.clear();
synthesizedJoints.clear();
for (int i = 0; i < program.basicBlockCount(); ++i) { for (int i = 0; i < program.basicBlockCount(); ++i) {
synthesizedPhis.add(new ArrayList<>()); synthesizedPhis.add(new ArrayList<>());
synthesizedJoints.add(new ArrayList<>());
} }
estimatePhis(); estimatePhis();
renameVariables(); renameVariables();
@ -122,12 +118,15 @@ public class PhiUpdater {
DefinitionExtractor definitionExtractor = new DefinitionExtractor(); DefinitionExtractor definitionExtractor = new DefinitionExtractor();
for (int i = 0; i < program.basicBlockCount(); ++i) { for (int i = 0; i < program.basicBlockCount(); ++i) {
currentBlock = program.basicBlockAt(i); currentBlock = program.basicBlockAt(i);
if (currentBlock.getExceptionVariable() != null) {
markAssignment(currentBlock.getExceptionVariable());
}
for (Phi phi : currentBlock.getPhis()) { for (Phi phi : currentBlock.getPhis()) {
markAssignment(phi.getReceiver()); markAssignment(phi.getReceiver());
} }
for (TryCatchJoint joint : currentBlock.getTryCatchJoints()) {
markAssignment(joint.getReceiver());
}
for (Instruction insn : currentBlock.getInstructions()) { for (Instruction insn : currentBlock.getInstructions()) {
insn.acceptVisitor(definitionExtractor); insn.acceptVisitor(definitionExtractor);
for (Variable var : definitionExtractor.getDefinedVariables()) { for (Variable var : definitionExtractor.getDefinedVariables()) {
@ -135,7 +134,9 @@ public class PhiUpdater {
} }
} }
for (TryCatchBlock tryCatch : currentBlock.getTryCatchBlocks()) { for (TryCatchBlock tryCatch : currentBlock.getTryCatchBlocks()) {
markAssignment(tryCatch.getExceptionVariable()); for (TryCatchJoint joint : tryCatch.getTryCatchJoints()) {
markAssignment(joint.getReceiver());
}
} }
} }
} }
@ -143,6 +144,7 @@ public class PhiUpdater {
private static class Task { private static class Task {
Variable[] variables; Variable[] variables;
BasicBlock block; BasicBlock block;
TryCatchBlock tryCatch;
} }
private void renameVariables() { private void renameVariables() {
@ -160,106 +162,66 @@ public class PhiUpdater {
} }
List<List<Incoming>> phiOutputs = ProgramUtils.getPhiOutputs(program); List<List<Incoming>> phiOutputs = ProgramUtils.getPhiOutputs(program);
List<List<TryCatchJoint>> existingOutputJoints = ProgramUtils.getOutputJoints(program);
boolean[] processed = new boolean[program.basicBlockCount()];
while (head > 0) { while (head > 0) {
Task task = stack[--head]; Task task = stack[--head];
currentBlock = task.block;
int index = currentBlock.getIndex();
if (processed[index]) {
continue;
}
processed[index] = true;
variableMap = Arrays.copyOf(task.variables, task.variables.length);
for (Phi phi : synthesizedPhis.get(index)) { if (task.tryCatch != null) {
Variable var = program.createVariable(); TryCatchBlock tryCatch = task.tryCatch;
var.getDebugNames().addAll(phi.getReceiver().getDebugNames()); if (domTree.dominates(tryCatch.getProtectedBlock().getIndex(), tryCatch.getHandler().getIndex())) {
propagateToTryCatch(phi.getReceiver(), var, null); Task next = new Task();
variableMap[phi.getReceiver().getIndex()] = var; next.block = tryCatch.getProtectedBlock();
phi.setReceiver(var); next.variables = Arrays.copyOf(variableMap, variableMap.length);
} for (TryCatchJoint joint : tryCatch.getTryCatchJoints()) {
for (TryCatchJoint joint : synthesizedJoints.get(index)) { Variable mappedReceiver = introduce(joint.getReceiver());
Variable var = program.createVariable(); for (Variable sourceVariable : joint.getSourceVariables()) {
var.getDebugNames().addAll(joint.getReceiver().getDebugNames()); next.variables[sourceVariable.getIndex()] = mappedReceiver;
variableMap[joint.getReceiver().getIndex()] = var; }
joint.setReceiver(var); next.variables[joint.getReceiver().getIndex()] = mappedReceiver;
} for (int i = 0; i < joint.getSourceVariables().size(); ++i) {
for (Phi phi : currentBlock.getPhis()) { Variable var = joint.getSourceVariables().get(i);
phi.setReceiver(define(phi.getReceiver())); joint.getSourceVariables().set(i, use(var));
} }
for (TryCatchJoint joint : currentBlock.getTryCatchJoints()) { joint.setReceiver(mappedReceiver);
joint.setReceiver(define(joint.getReceiver())); }
} stack[head++] = next;
for (Instruction insn : currentBlock.getInstructions()) {
insn.acceptVisitor(consumer);
}
Map<BasicBlock, Map<Variable, Variable>> tryCatchVariableMap = new HashMap<>();
for (TryCatchBlock tryCatch : currentBlock.getTryCatchBlocks()) {
Map<Variable, Variable> catchVariableMap = new HashMap<>();
tryCatchVariableMap.put(tryCatch.getHandler(), catchVariableMap);
Variable var = tryCatch.getExceptionVariable();
if (var != null) {
Variable newVar = introduce(var);
tryCatch.setExceptionVariable(newVar);
catchVariableMap.put(var, newVar);
} }
} renameOutgoingPhis(tryCatch.getHandler().getIndex());
} else {
currentBlock = task.block;
int index = currentBlock.getIndex();
variableMap = Arrays.copyOf(task.variables, task.variables.length);
for (Incoming output : phiOutputs.get(index)) { for (Phi phi : synthesizedPhis.get(index)) {
Variable var = output.getValue(); Variable var = program.createVariable();
Variable exceptionVar = tryCatchVariableMap var.getDebugNames().addAll(phi.getReceiver().getDebugNames());
.getOrDefault(output.getPhi().getBasicBlock(), Collections.emptyMap()) propagateToTryCatch(phi.getReceiver(), var, null);
.get(var); variableMap[phi.getReceiver().getIndex()] = var;
output.setValue(exceptionVar != null ? exceptionVar : use(var)); phi.setReceiver(var);
} }
for (TryCatchJoint joint : existingOutputJoints.get(index)) { for (Phi phi : currentBlock.getPhis()) {
for (int i = 0; i < joint.getSourceVariables().size(); ++i) { phi.setReceiver(define(phi.getReceiver()));
Variable var = joint.getSourceVariables().get(i);
Variable exceptionVar = tryCatchVariableMap
.getOrDefault(joint.getBlock(), Collections.emptyMap())
.get(var);
joint.getSourceVariables().set(i, exceptionVar != null ? exceptionVar : use(var));
} }
}
int[] successors = domGraph.outgoingEdges(index); for (Instruction insn : currentBlock.getInstructions()) {
for (int successor : successors) { insn.acceptVisitor(consumer);
Task next = new Task();
next.variables = Arrays.copyOf(variableMap, variableMap.length);
next.block = program.basicBlockAt(successor);
Map<Variable, Variable> catchVariableMap = tryCatchVariableMap.get(next.block);
if (catchVariableMap != null) {
for (Map.Entry<Variable, Variable> entry : catchVariableMap.entrySet()) {
next.variables[entry.getKey().getIndex()] = entry.getValue();
}
} }
stack[head++] = next;
} for (Incoming output : phiOutputs.get(index)) {
successors = cfg.outgoingEdges(index); Variable var = output.getValue();
for (int successor : successors) { output.setValue(use(var));
int[] phiIndexes = phiIndexMap[successor]; }
List<Phi> phis = synthesizedPhis.get(successor);
Map<Variable, Variable> catchVariableMap = tryCatchVariableMap.get(program.basicBlockAt(successor)); int[] successors = domGraph.outgoingEdges(index);
for (int j = 0; j < phis.size(); ++j) { for (int successor : successors) {
Phi phi = phis.get(j); Task next = new Task();
Variable var = null; next.variables = Arrays.copyOf(variableMap, variableMap.length);
if (catchVariableMap != null) { next.block = program.basicBlockAt(successor);
var = catchVariableMap.get(program.variableAt(phiIndexes[j])); stack[head++] = next;
} }
if (var == null) { successors = cfg.outgoingEdges(index);
var = variableMap[phiIndexes[j]]; for (int successor : successors) {
} renameOutgoingPhis(successor);
if (var != null) {
Incoming incoming = new Incoming();
incoming.setSource(currentBlock);
incoming.setValue(var);
phi.getIncomings().add(incoming);
phi.getReceiver().getDebugNames().addAll(var.getDebugNames());
}
} }
} }
} }
@ -270,18 +232,27 @@ public class PhiUpdater {
program.basicBlockAt(i).getPhis().add(phi); program.basicBlockAt(i).getPhis().add(phi);
} }
} }
for (TryCatchJoint joint : synthesizedJoints.get(i)) { }
if (!joint.getSourceVariables().isEmpty()) { }
program.basicBlockAt(i).getTryCatchJoints().add(joint);
} private void renameOutgoingPhis(int successor) {
int[] phiIndexes = phiIndexMap[successor];
List<Phi> phis = synthesizedPhis.get(successor);
for (int j = 0; j < phis.size(); ++j) {
Phi phi = phis.get(j);
Variable var = variableMap[phiIndexes[j]];
if (var != null) {
Incoming incoming = new Incoming();
incoming.setSource(currentBlock);
incoming.setValue(var);
phi.getIncomings().add(incoming);
phi.getReceiver().getDebugNames().addAll(var.getDebugNames());
} }
} }
} }
private void markAssignment(Variable var) { private void markAssignment(Variable var) {
boolean fromHandler = currentBlock.getTryCatchBlocks().stream().anyMatch(
tryCatch -> tryCatch.getExceptionVariable() == var);
BasicBlock[] worklist = new BasicBlock[program.basicBlockCount() * 4]; BasicBlock[] worklist = new BasicBlock[program.basicBlockCount() * 4];
int head = 0; int head = 0;
worklist[head++] = currentBlock; worklist[head++] = currentBlock;
@ -294,9 +265,6 @@ public class PhiUpdater {
for (int frontier : frontiers) { for (int frontier : frontiers) {
BasicBlock frontierBlock = program.basicBlockAt(frontier); BasicBlock frontierBlock = program.basicBlockAt(frontier);
if (!fromHandler && isExceptionHandler(block, frontierBlock)) {
continue;
}
boolean exists = frontierBlock.getPhis().stream() boolean exists = frontierBlock.getPhis().stream()
.flatMap(phi -> phi.getIncomings().stream()) .flatMap(phi -> phi.getIncomings().stream())
@ -315,56 +283,9 @@ public class PhiUpdater {
worklist[head++] = frontierBlock; worklist[head++] = frontierBlock;
} }
} }
if (!fromHandler) {
for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
if (tryCatch.getExceptionVariable() == var || !hasReassignmentTo(block, var)) {
continue;
}
BasicBlock frontierBlock = tryCatch.getHandler();
int frontier = frontierBlock.getIndex();
boolean exists = frontierBlock.getTryCatchJoints().stream()
.anyMatch(joint -> joint.getSourceVariables().contains(var) && joint.getSource() == block);
if (exists) {
continue;
}
Map<Variable, TryCatchJoint> jointSubmap = jointMap.get(frontier).get(block);
if (jointSubmap == null) {
jointSubmap = new HashMap<>();
jointMap.get(frontier).put(block, jointSubmap);
}
TryCatchJoint joint = jointSubmap.get(var);
if (joint == null) {
joint = new TryCatchJoint();
joint.setSource(block);
joint.setReceiver(var);
synthesizedJoints.get(frontier).add(joint);
jointSubmap.put(var, joint);
worklist[head++] = frontierBlock;
}
}
}
} }
} }
private boolean hasReassignmentTo(BasicBlock block, Variable var) {
DefinitionExtractor defExtractor = new DefinitionExtractor();
for (int i = 0; i < block.instructionCount(); ++i) {
block.getInstructions().get(i).acceptVisitor(defExtractor);
for (Variable definedVar : defExtractor.getDefinedVariables()) {
if (definedVar == var) {
return true;
}
}
}
return false;
}
private boolean isExceptionHandler(BasicBlock source, BasicBlock target) {
return source.getTryCatchBlocks().stream().anyMatch(tryCatch -> tryCatch.getHandler() == target);
}
private Variable define(Variable var) { private Variable define(Variable var) {
Variable old = variableMap[var.getIndex()]; Variable old = variableMap[var.getIndex()];
Variable original = var; Variable original = var;
@ -376,9 +297,10 @@ public class PhiUpdater {
private void propagateToTryCatch(Variable original, Variable var, Variable old) { private void propagateToTryCatch(Variable original, Variable var, Variable old) {
for (TryCatchBlock tryCatch : currentBlock.getTryCatchBlocks()) { for (TryCatchBlock tryCatch : currentBlock.getTryCatchBlocks()) {
if (tryCatch.getExceptionVariable() == original) { if (tryCatch.getHandler().getExceptionVariable() == original) {
continue; continue;
} }
Map<Variable, TryCatchJoint> joints = jointMap.get(tryCatch.getHandler().getIndex()).get(currentBlock); Map<Variable, TryCatchJoint> joints = jointMap.get(tryCatch.getHandler().getIndex()).get(currentBlock);
if (joints == null) { if (joints == null) {
continue; continue;

View File

@ -78,10 +78,12 @@ public final class ProgramUtils {
for (int i = 0; i < program.basicBlockCount(); ++i) { for (int i = 0; i < program.basicBlockCount(); ++i) {
BasicBlockReader block = program.basicBlockAt(i); BasicBlockReader block = program.basicBlockAt(i);
BasicBlock blockCopy = copy.basicBlockAt(i); BasicBlock blockCopy = copy.basicBlockAt(i);
if (block.getExceptionVariable() != null) {
blockCopy.setExceptionVariable(copy.variableAt(block.getExceptionVariable().getIndex()));
}
blockCopy.getInstructions().addAll(copyInstructions(block, 0, block.instructionCount(), copy)); blockCopy.getInstructions().addAll(copyInstructions(block, 0, block.instructionCount(), copy));
blockCopy.getPhis().addAll(copyPhis(block, copy)); blockCopy.getPhis().addAll(copyPhis(block, copy));
blockCopy.getTryCatchBlocks().addAll(copyTryCatches(block, copy)); blockCopy.getTryCatchBlocks().addAll(copyTryCatches(block, copy));
blockCopy.getTryCatchJoints().addAll(copyTryCatchJoints(block, copy));
} }
return copy; return copy;
} }
@ -117,18 +119,17 @@ public final class ProgramUtils {
for (TryCatchBlockReader tryCatch : block.readTryCatchBlocks()) { for (TryCatchBlockReader tryCatch : block.readTryCatchBlocks()) {
TryCatchBlock tryCatchCopy = new TryCatchBlock(); TryCatchBlock tryCatchCopy = new TryCatchBlock();
tryCatchCopy.setExceptionType(tryCatch.getExceptionType()); tryCatchCopy.setExceptionType(tryCatch.getExceptionType());
tryCatchCopy.setExceptionVariable(target.variableAt(tryCatch.getExceptionVariable().getIndex()));
tryCatchCopy.setHandler(target.basicBlockAt(tryCatch.getHandler().getIndex())); tryCatchCopy.setHandler(target.basicBlockAt(tryCatch.getHandler().getIndex()));
tryCatchCopy.getTryCatchJoints().addAll(copyTryCatchJoints(tryCatch, target));
result.add(tryCatchCopy); result.add(tryCatchCopy);
} }
return result; return result;
} }
public static List<TryCatchJoint> copyTryCatchJoints(BasicBlockReader block, Program target) { public static List<TryCatchJoint> copyTryCatchJoints(TryCatchBlockReader block, Program target) {
List<TryCatchJoint> result = new ArrayList<>(); List<TryCatchJoint> result = new ArrayList<>();
for (TryCatchJointReader joint : block.readTryCatchJoints()) { for (TryCatchJointReader joint : block.readTryCatchJoints()) {
TryCatchJoint jointCopy = new TryCatchJoint(); TryCatchJoint jointCopy = new TryCatchJoint();
jointCopy.setSource(target.basicBlockAt(joint.getSource().getIndex()));
jointCopy.setReceiver(target.variableAt(joint.getReceiver().getIndex())); jointCopy.setReceiver(target.variableAt(joint.getReceiver().getIndex()));
for (VariableReader sourceVar : joint.readSourceVariables()) { for (VariableReader sourceVar : joint.readSourceVariables()) {
jointCopy.getSourceVariables().add(target.variableAt(sourceVar.getIndex())); jointCopy.getSourceVariables().add(target.variableAt(sourceVar.getIndex()));
@ -156,43 +157,31 @@ public final class ProgramUtils {
return outputs; return outputs;
} }
public static List<List<TryCatchJoint>> getOutputJoints(Program program) {
List<List<TryCatchJoint>> outputs = new ArrayList<>(program.basicBlockCount());
for (int i = 0; i < program.basicBlockCount(); ++i) {
outputs.add(new ArrayList<>());
}
for (int i = 0; i < program.basicBlockCount(); ++i) {
BasicBlock block = program.basicBlockAt(i);
for (TryCatchJoint joint : block.getTryCatchJoints()) {
outputs.get(joint.getSource().getIndex()).add(joint);
}
}
return outputs;
}
public static BasicBlock[] getVariableDefinitionPlaces(Program program) { public static BasicBlock[] getVariableDefinitionPlaces(Program program) {
BasicBlock[] places = new BasicBlock[program.variableCount()]; BasicBlock[] places = new BasicBlock[program.variableCount()];
DefinitionExtractor defExtractor = new DefinitionExtractor(); DefinitionExtractor defExtractor = new DefinitionExtractor();
for (int i = 0; i < program.basicBlockCount(); ++i) { for (int i = 0; i < program.basicBlockCount(); ++i) {
BasicBlock block = program.basicBlockAt(i); BasicBlock block = program.basicBlockAt(i);
Variable exceptionVar = block.getExceptionVariable();
if (exceptionVar != null) {
places[exceptionVar.getIndex()] = block;
}
for (Phi phi : block.getPhis()) { for (Phi phi : block.getPhis()) {
places[phi.getReceiver().getIndex()] = block; places[phi.getReceiver().getIndex()] = block;
} }
for (TryCatchJoint joint : block.getTryCatchJoints()) {
places[joint.getReceiver().getIndex()] = block;
}
for (Instruction insn : block.getInstructions()) { for (Instruction insn : block.getInstructions()) {
insn.acceptVisitor(defExtractor); insn.acceptVisitor(defExtractor);
for (Variable var : defExtractor.getDefinedVariables()) { for (Variable var : defExtractor.getDefinedVariables()) {
places[var.getIndex()] = block; places[var.getIndex()] = block;
} }
} }
for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) { for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
Variable var = tryCatch.getExceptionVariable(); for (TryCatchJoint joint : tryCatch.getTryCatchJoints()) {
if (var != null) { places[joint.getReceiver().getIndex()] = block;
places[var.getIndex()] = tryCatch.getHandler();
} }
} }
} }

View File

@ -33,6 +33,7 @@ import org.teavm.model.MethodReference;
import org.teavm.model.Phi; import org.teavm.model.Phi;
import org.teavm.model.Program; import org.teavm.model.Program;
import org.teavm.model.ProgramReader; import org.teavm.model.ProgramReader;
import org.teavm.model.TryCatchBlock;
import org.teavm.model.TryCatchJoint; import org.teavm.model.TryCatchJoint;
import org.teavm.model.Variable; import org.teavm.model.Variable;
import org.teavm.model.instructions.AssignInstruction; import org.teavm.model.instructions.AssignInstruction;
@ -77,7 +78,9 @@ public class RegisterAllocator {
for (int i = 0; i < program.basicBlockCount(); ++i) { for (int i = 0; i < program.basicBlockCount(); ++i) {
program.basicBlockAt(i).getPhis().clear(); program.basicBlockAt(i).getPhis().clear();
program.basicBlockAt(i).getTryCatchJoints().clear(); for (TryCatchBlock tryCatch : program.basicBlockAt(i).getTryCatchBlocks()) {
tryCatch.getTryCatchJoints().clear();
}
} }
} }
@ -116,14 +119,16 @@ public class RegisterAllocator {
private void insertJointArgumentsCopies(Program program) { private void insertJointArgumentsCopies(Program program) {
for (int i = 0; i < program.basicBlockCount(); ++i) { for (int i = 0; i < program.basicBlockCount(); ++i) {
BasicBlock block = program.basicBlockAt(i); BasicBlock block = program.basicBlockAt(i);
block.getTryCatchJoints().forEach(this::insertCopy); for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
tryCatch.getTryCatchJoints().forEach(this::insertCopy);
}
} }
} }
private void insertCopy(TryCatchJoint joint) { private void insertCopy(TryCatchJoint joint) {
Set<Variable> variableSet = new HashSet<>(joint.getSourceVariables()); Set<Variable> variableSet = new HashSet<>(joint.getSourceVariables());
BasicBlock block = joint.getSource(); BasicBlock block = joint.getBlock().getProtectedBlock();
DefinitionExtractor defExtractor = new DefinitionExtractor(); DefinitionExtractor defExtractor = new DefinitionExtractor();
for (int i = block.getInstructions().size() - 1; i >= 0; --i) { for (int i = block.getInstructions().size() - 1; i >= 0; --i) {
Instruction insn = block.getInstructions().get(i); Instruction insn = block.getInstructions().get(i);
@ -151,16 +156,12 @@ public class RegisterAllocator {
Map<BasicBlock, BasicBlock> blockMap = new HashMap<>(); Map<BasicBlock, BasicBlock> blockMap = new HashMap<>();
for (Phi phi : program.basicBlockAt(i).getPhis()) { for (Phi phi : program.basicBlockAt(i).getPhis()) {
for (Incoming incoming : phi.getIncomings()) { for (Incoming incoming : phi.getIncomings()) {
if (!isExceptionHandler(incoming)) { insertCopy(incoming, blockMap);
insertCopy(incoming, blockMap);
}
} }
} }
for (Phi phi : program.basicBlockAt(i).getPhis()) { for (Phi phi : program.basicBlockAt(i).getPhis()) {
for (Incoming incoming : phi.getIncomings()) { for (Incoming incoming : phi.getIncomings()) {
if (!isExceptionHandler(incoming)) { insertCopy(incoming, blockMap);
insertCopy(incoming, blockMap);
}
} }
} }
} }
@ -194,11 +195,6 @@ public class RegisterAllocator {
incoming.setValue(copyInstruction.getReceiver()); incoming.setValue(copyInstruction.getReceiver());
} }
private boolean isExceptionHandler(Incoming incoming) {
return incoming.getSource().getTryCatchJoints().stream().anyMatch(
joint -> joint.getReceiver() == incoming.getValue());
}
private void removeRedundantCopies(Program program, List<MutableGraphNode> interferenceGraph, private void removeRedundantCopies(Program program, List<MutableGraphNode> interferenceGraph,
DisjointSet congruenceClasses) { DisjointSet congruenceClasses) {
for (int i = 0; i < program.basicBlockCount(); ++i) { for (int i = 0; i < program.basicBlockCount(); ++i) {
@ -301,9 +297,11 @@ public class RegisterAllocator {
classes.union(phi.getReceiver().getIndex(), incoming.getValue().getIndex()); classes.union(phi.getReceiver().getIndex(), incoming.getValue().getIndex());
} }
} }
for (TryCatchJoint joint : block.getTryCatchJoints()) { for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
for (Variable sourceVar : joint.getSourceVariables()) { for (TryCatchJoint joint : tryCatch.getTryCatchJoints()) {
classes.union(sourceVar.getIndex(), joint.getReceiver().getIndex()); for (Variable sourceVar : joint.getSourceVariables()) {
classes.union(sourceVar.getIndex(), joint.getReceiver().getIndex());
}
} }
} }
} }

View File

@ -22,10 +22,6 @@ import org.teavm.common.IntegerStack;
import org.teavm.model.*; import org.teavm.model.*;
import org.teavm.model.instructions.*; import org.teavm.model.instructions.*;
/**
*
* @author Alexey Andreev
*/
public class TypeInferer { public class TypeInferer {
VariableType[] types; VariableType[] types;
GraphBuilder builder; GraphBuilder builder;
@ -45,20 +41,23 @@ public class TypeInferer {
arrayElemBuilder = new GraphBuilder(sz); arrayElemBuilder = new GraphBuilder(sz);
for (int i = 0; i < program.basicBlockCount(); ++i) { for (int i = 0; i < program.basicBlockCount(); ++i) {
BasicBlockReader block = program.basicBlockAt(i); BasicBlockReader block = program.basicBlockAt(i);
if (block.getExceptionVariable() != null) {
types[block.getExceptionVariable().getIndex()] = VariableType.OBJECT;
}
block.readAllInstructions(reader); block.readAllInstructions(reader);
for (PhiReader phi : block.readPhis()) { for (PhiReader phi : block.readPhis()) {
for (IncomingReader incoming : phi.readIncomings()) { for (IncomingReader incoming : phi.readIncomings()) {
builder.addEdge(incoming.getValue().getIndex(), phi.getReceiver().getIndex()); builder.addEdge(incoming.getValue().getIndex(), phi.getReceiver().getIndex());
} }
} }
for (TryCatchJointReader joint : block.readTryCatchJoints()) {
for (VariableReader sourceVar : joint.readSourceVariables()) {
builder.addEdge(sourceVar.getIndex(), joint.getReceiver().getIndex());
}
}
for (TryCatchBlockReader tryCatch : block.readTryCatchBlocks()) { for (TryCatchBlockReader tryCatch : block.readTryCatchBlocks()) {
if (tryCatch.getExceptionVariable() != null) { for (TryCatchJointReader joint : tryCatch.readTryCatchJoints()) {
types[tryCatch.getExceptionVariable().getIndex()] = VariableType.OBJECT; for (VariableReader sourceVar : joint.readSourceVariables()) {
builder.addEdge(sourceVar.getIndex(), joint.getReceiver().getIndex());
}
} }
} }
} }

View File

@ -32,8 +32,8 @@ public class EmptyBlockElimination implements MethodOptimization {
int lastNonEmpty = program.basicBlockCount() - 1; int lastNonEmpty = program.basicBlockCount() - 1;
for (int i = program.basicBlockCount() - 2; i > 0; --i) { for (int i = program.basicBlockCount() - 2; i > 0; --i) {
BasicBlock block = program.basicBlockAt(i); BasicBlock block = program.basicBlockAt(i);
if (block.getPhis().isEmpty() && block.getTryCatchJoints().isEmpty() if (block.getPhis().isEmpty() && block.getInstructions().size() == 1
&& block.getInstructions().size() == 1 && block.getLastInstruction() instanceof JumpInstruction) { && block.getLastInstruction() instanceof JumpInstruction) {
JumpInstruction insn = (JumpInstruction) block.getLastInstruction(); JumpInstruction insn = (JumpInstruction) block.getLastInstruction();
if (insn.getTarget().getIndex() == i + 1) { if (insn.getTarget().getIndex() == i + 1) {
blockMapping[i] = lastNonEmpty; blockMapping[i] = lastNonEmpty;

View File

@ -54,7 +54,6 @@ public class GlobalValueNumbering implements MethodOptimization {
map[i] = i; map[i] = i;
} }
List<List<Incoming>> outgoings = ProgramUtils.getPhiOutputs(program); List<List<Incoming>> outgoings = ProgramUtils.getPhiOutputs(program);
List<List<TryCatchJoint>> outputJoints = ProgramUtils.getOutputJoints(program);
int[] stack = new int[cfg.size() * 2]; int[] stack = new int[cfg.size() * 2];
int top = 0; int top = 0;
@ -89,6 +88,12 @@ public class GlobalValueNumbering implements MethodOptimization {
block.getPhis().remove(i--); block.getPhis().remove(i--);
} }
}*/ }*/
if (block.getExceptionVariable() != null) {
int var = map[block.getExceptionVariable().getIndex()];
block.setExceptionVariable(program.variableAt(var));
}
for (int i = 0; i < block.getInstructions().size(); ++i) { for (int i = 0; i < block.getInstructions().size(); ++i) {
evaluatedConstant = null; evaluatedConstant = null;
Instruction currentInsn = block.getInstructions().get(i); Instruction currentInsn = block.getInstructions().get(i);
@ -127,22 +132,19 @@ public class GlobalValueNumbering implements MethodOptimization {
} }
} }
} }
for (TryCatchJoint joint : outputJoints.get(v)) { for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
for (int i = 0; i < joint.getSourceVariables().size(); ++i) { for (TryCatchJoint joint : tryCatch.getTryCatchJoints()) {
int sourceVar = map[joint.getSourceVariables().get(i).getIndex()]; for (int i = 0; i < joint.getSourceVariables().size(); ++i) {
joint.getSourceVariables().set(i, program.variableAt(sourceVar)); int sourceVar = map[joint.getSourceVariables().get(i).getIndex()];
joint.getSourceVariables().set(i, program.variableAt(sourceVar));
}
} }
} }
for (Incoming incoming : outgoings.get(v)) { for (Incoming incoming : outgoings.get(v)) {
int value = map[incoming.getValue().getIndex()]; int value = map[incoming.getValue().getIndex()];
incoming.setValue(program.variableAt(value)); incoming.setValue(program.variableAt(value));
} }
for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
if (tryCatch.getExceptionVariable() != null) {
int var = map[tryCatch.getExceptionVariable().getIndex()];
tryCatch.setExceptionVariable(program.variableAt(var));
}
}
for (int succ : dom.outgoingEdges(v)) { for (int succ : dom.outgoingEdges(v)) {
stack[top++] = succ; stack[top++] = succ;
} }

View File

@ -166,6 +166,10 @@ public class Inlining {
} }
private void copyInlinedBlock(BasicBlock source, BasicBlock target) { private void copyInlinedBlock(BasicBlock source, BasicBlock target) {
if (source.getExceptionVariable() != null) {
target.setExceptionVariable(source.getExceptionVariable());
}
InstructionCopyReader insnCopier = new InstructionCopyReader(target.getProgram()); InstructionCopyReader insnCopier = new InstructionCopyReader(target.getProgram());
for (int i = 0; i < source.instructionCount(); ++i) { for (int i = 0; i < source.instructionCount(); ++i) {
source.readInstruction(i, insnCopier); source.readInstruction(i, insnCopier);
@ -190,18 +194,15 @@ public class Inlining {
TryCatchBlock tryCatchCopy = new TryCatchBlock(); TryCatchBlock tryCatchCopy = new TryCatchBlock();
int handler = tryCatch.getHandler().getIndex(); int handler = tryCatch.getHandler().getIndex();
tryCatchCopy.setExceptionType(tryCatch.getExceptionType()); tryCatchCopy.setExceptionType(tryCatch.getExceptionType());
tryCatchCopy.setExceptionVariable(tryCatch.getExceptionVariable());
tryCatchCopy.setHandler(target.getProgram().basicBlockAt(handler)); tryCatchCopy.setHandler(target.getProgram().basicBlockAt(handler));
target.getTryCatchBlocks().add(tryCatchCopy); target.getTryCatchBlocks().add(tryCatchCopy);
}
for (TryCatchJoint joint : source.getTryCatchJoints()) { for (TryCatchJoint joint : tryCatch.getTryCatchJoints()) {
TryCatchJoint jointCopy = new TryCatchJoint(); TryCatchJoint jointCopy = new TryCatchJoint();
jointCopy.setReceiver(joint.getReceiver()); jointCopy.setReceiver(joint.getReceiver());
jointCopy.getSourceVariables().addAll(joint.getSourceVariables()); jointCopy.getSourceVariables().addAll(joint.getSourceVariables());
int sourceIndex = joint.getSource().getIndex(); tryCatchCopy.getTryCatchJoints().add(joint);
jointCopy.setSource(target.getProgram().basicBlockAt(sourceIndex)); }
target.getTryCatchJoints().add(joint);
} }
} }
@ -211,18 +212,6 @@ public class Inlining {
TryCatchBlock tryCatchCopy = new TryCatchBlock(); TryCatchBlock tryCatchCopy = new TryCatchBlock();
tryCatchCopy.setExceptionType(tryCatch.getExceptionType()); tryCatchCopy.setExceptionType(tryCatch.getExceptionType());
tryCatchCopy.setHandler(tryCatch.getHandler()); tryCatchCopy.setHandler(tryCatch.getHandler());
tryCatchCopy.setExceptionVariable(source.getProgram().createVariable());
List<Incoming> handlerIncomings = tryCatch.getHandler().getPhis().stream()
.flatMap(phi -> phi.getIncomings().stream())
.filter(incoming -> incoming.getValue() == tryCatch.getExceptionVariable()
&& incoming.getSource() == source)
.collect(Collectors.toList());
for (Incoming incoming : handlerIncomings) {
Incoming incomingCopy = new Incoming();
incomingCopy.setValue(tryCatchCopy.getExceptionVariable());
incomingCopy.setSource(target);
incoming.getPhi().getIncomings().add(incomingCopy);
}
copiedTryCatches.add(tryCatchCopy); copiedTryCatches.add(tryCatchCopy);
} }
target.getTryCatchBlocks().addAll(copiedTryCatches); target.getTryCatchBlocks().addAll(copiedTryCatches);

View File

@ -317,6 +317,9 @@ class LoopInversionImpl {
for (int node : copiedNodes.keys().toArray()) { for (int node : copiedNodes.keys().toArray()) {
BasicBlock sourceBlock = program.basicBlockAt(node); BasicBlock sourceBlock = program.basicBlockAt(node);
BasicBlock targetBlock = program.basicBlockAt(copiedNodes.get(node)); BasicBlock targetBlock = program.basicBlockAt(copiedNodes.get(node));
targetBlock.setExceptionVariable(sourceBlock.getExceptionVariable());
copier.resetLocation(); copier.resetLocation();
for (int i = 0; i < sourceBlock.instructionCount(); ++i) { for (int i = 0; i < sourceBlock.instructionCount(); ++i) {
sourceBlock.readInstruction(i, copier); sourceBlock.readInstruction(i, copier);
@ -342,18 +345,15 @@ class LoopInversionImpl {
TryCatchBlock tryCatchCopy = new TryCatchBlock(); TryCatchBlock tryCatchCopy = new TryCatchBlock();
int handler = tryCatch.getHandler().getIndex(); int handler = tryCatch.getHandler().getIndex();
tryCatchCopy.setExceptionType(tryCatch.getExceptionType()); tryCatchCopy.setExceptionType(tryCatch.getExceptionType());
tryCatchCopy.setExceptionVariable(tryCatch.getExceptionVariable());
tryCatchCopy.setHandler(program.basicBlockAt(copiedNodes.getOrDefault(handler, handler))); tryCatchCopy.setHandler(program.basicBlockAt(copiedNodes.getOrDefault(handler, handler)));
targetBlock.getTryCatchBlocks().add(tryCatchCopy); targetBlock.getTryCatchBlocks().add(tryCatchCopy);
}
for (TryCatchJoint joint : sourceBlock.getTryCatchJoints()) { for (TryCatchJoint joint : tryCatch.getTryCatchJoints()) {
TryCatchJoint jointCopy = new TryCatchJoint(); TryCatchJoint jointCopy = new TryCatchJoint();
jointCopy.setReceiver(joint.getReceiver()); jointCopy.setReceiver(joint.getReceiver());
jointCopy.getSourceVariables().addAll(joint.getSourceVariables()); jointCopy.getSourceVariables().addAll(joint.getSourceVariables());
int source = joint.getSource().getIndex(); tryCatchCopy.getTryCatchJoints().add(joint);
jointCopy.setSource(program.basicBlockAt(copiedNodes.getOrDefault(source, source))); }
targetBlock.getTryCatchJoints().add(joint);
} }
} }

View File

@ -60,12 +60,16 @@ public class UnusedVariableElimination implements MethodOptimization {
block.getInstructions().remove(j--); block.getInstructions().remove(j--);
} }
} }
for (int j = 0; j < block.getTryCatchJoints().size(); ++j) {
TryCatchJoint joint = block.getTryCatchJoints().get(j); for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
if (!used[joint.getReceiver().getIndex()]) { for (int j = 0; j < tryCatch.getTryCatchJoints().size(); ++j) {
block.getTryCatchJoints().remove(j--); TryCatchJoint joint = tryCatch.getTryCatchJoints().get(j);
if (!used[joint.getReceiver().getIndex()]) {
tryCatch.getTryCatchJoints().remove(j--);
}
} }
} }
for (int j = 0; j < block.getPhis().size(); ++j) { for (int j = 0; j < block.getPhis().size(); ++j) {
Phi phi = block.getPhis().get(j); Phi phi = block.getPhis().get(j);
if (!used[phi.getReceiver().getIndex()]) { if (!used[phi.getReceiver().getIndex()]) {

View File

@ -37,9 +37,11 @@ public final class VariableUsageGraphBuilder {
builder.addEdge(incoming.getValue().getIndex(), phi.getReceiver().getIndex()); builder.addEdge(incoming.getValue().getIndex(), phi.getReceiver().getIndex());
} }
} }
for (TryCatchJoint joint : block.getTryCatchJoints()) { for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
for (Variable sourceVar : joint.getSourceVariables()) { for (TryCatchJoint joint : tryCatch.getTryCatchJoints()) {
builder.addEdge(sourceVar.getIndex(), joint.getReceiver().getIndex()); for (Variable sourceVar : joint.getSourceVariables()) {
builder.addEdge(sourceVar.getIndex(), joint.getReceiver().getIndex());
}
} }
} }
} }

View File

@ -240,6 +240,7 @@ public class ProgramParser {
workStack.push(new Step(index, next)); workStack.push(new Step(index, next));
} }
} }
for (Object obj : method.tryCatchBlocks) { for (Object obj : method.tryCatchBlocks) {
TryCatchBlockNode tryCatchNode = (TryCatchBlockNode) obj; TryCatchBlockNode tryCatchNode = (TryCatchBlockNode) obj;
if (tryCatchNode.start == tryCatchNode.handler) { if (tryCatchNode.start == tryCatchNode.handler) {
@ -257,7 +258,7 @@ public class ProgramParser {
tryCatch.setExceptionType(tryCatchNode.type.replace('/', '.')); tryCatch.setExceptionType(tryCatchNode.type.replace('/', '.'));
} }
tryCatch.setHandler(getBasicBlock(labelIndexes.get(tryCatchNode.handler.getLabel()))); tryCatch.setHandler(getBasicBlock(labelIndexes.get(tryCatchNode.handler.getLabel())));
tryCatch.setExceptionVariable(getVariable(minLocal + method.maxLocals)); tryCatch.getHandler().setExceptionVariable(program.variableAt(minLocal + method.maxLocals));
block.getTryCatchBlocks().add(tryCatch); block.getTryCatchBlocks().add(tryCatch);
} }
} }

View File

@ -29,6 +29,7 @@ import org.teavm.model.MethodReference;
import org.teavm.model.PhiReader; import org.teavm.model.PhiReader;
import org.teavm.model.ProgramReader; import org.teavm.model.ProgramReader;
import org.teavm.model.RuntimeConstant; import org.teavm.model.RuntimeConstant;
import org.teavm.model.TryCatchBlockReader;
import org.teavm.model.TryCatchJointReader; import org.teavm.model.TryCatchJointReader;
import org.teavm.model.ValueType; import org.teavm.model.ValueType;
import org.teavm.model.VariableReader; import org.teavm.model.VariableReader;
@ -65,12 +66,14 @@ class AliasFinder {
set.union(inputs.iterator().next(), phi.getReceiver().getIndex()); set.union(inputs.iterator().next(), phi.getReceiver().getIndex());
} }
} }
for (TryCatchJointReader joint : block.readTryCatchJoints()) { for (TryCatchBlockReader tryCatch : block.readTryCatchBlocks()) {
Set<Integer> inputs = joint.readSourceVariables().stream() for (TryCatchJointReader joint : tryCatch.readTryCatchJoints()) {
.map(sourceVar -> sourceVar.getIndex()) Set<Integer> inputs = joint.readSourceVariables().stream()
.collect(Collectors.toSet()); .map(sourceVar -> sourceVar.getIndex())
if (inputs.size() == 1) { .collect(Collectors.toSet());
set.union(inputs.iterator().next(), joint.getReceiver().getIndex()); if (inputs.size() == 1) {
set.union(inputs.iterator().next(), joint.getReceiver().getIndex());
}
} }
} }
} }

View File

@ -183,11 +183,13 @@ public class CompositeMethodGenerator {
BasicBlockReader templateBlock = template.basicBlockAt(i); BasicBlockReader templateBlock = template.basicBlockAt(i);
blockIndex = i == 0 ? startBlock : substitutor.blockOffset + i; blockIndex = i == 0 ? startBlock : substitutor.blockOffset + i;
BasicBlock targetBlock = program.basicBlockAt(blockIndex); BasicBlock targetBlock = program.basicBlockAt(blockIndex);
if (templateBlock.getExceptionVariable() != null) {
targetBlock.setExceptionVariable(substitutor.var(templateBlock.getExceptionVariable()));
}
for (TryCatchBlockReader templateTryCatch : templateBlock.readTryCatchBlocks()) { for (TryCatchBlockReader templateTryCatch : templateBlock.readTryCatchBlocks()) {
TryCatchBlock tryCatch = new TryCatchBlock(); TryCatchBlock tryCatch = new TryCatchBlock();
tryCatch.setExceptionType(templateTryCatch.getExceptionType()); tryCatch.setExceptionType(templateTryCatch.getExceptionType());
tryCatch.setExceptionVariable(substitutor.var(templateTryCatch.getExceptionVariable()));
tryCatch.setHandler(substitutor.block(templateTryCatch.getHandler())); tryCatch.setHandler(substitutor.block(templateTryCatch.getHandler()));
targetBlock.getTryCatchBlocks().add(tryCatch); targetBlock.getTryCatchBlocks().add(tryCatch);
} }

View File

@ -29,6 +29,7 @@ import org.teavm.model.MethodReference;
import org.teavm.model.Phi; import org.teavm.model.Phi;
import org.teavm.model.PrimitiveType; import org.teavm.model.PrimitiveType;
import org.teavm.model.Program; import org.teavm.model.Program;
import org.teavm.model.TryCatchBlock;
import org.teavm.model.TryCatchJoint; import org.teavm.model.TryCatchJoint;
import org.teavm.model.ValueType; import org.teavm.model.ValueType;
import org.teavm.model.Variable; import org.teavm.model.Variable;
@ -111,9 +112,11 @@ public class BoxingElimination {
union(phi.getReceiver().getIndex(), incoming.getValue().getIndex()); union(phi.getReceiver().getIndex(), incoming.getValue().getIndex());
} }
} }
for (TryCatchJoint joint : block.getTryCatchJoints()) { for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
for (Variable sourceVar : joint.getSourceVariables()) { for (TryCatchJoint joint : tryCatch.getTryCatchJoints()) {
union(sourceVar.getIndex(), joint.getReceiver().getIndex()); for (Variable sourceVar : joint.getSourceVariables()) {
union(sourceVar.getIndex(), joint.getReceiver().getIndex());
}
} }
} }
} }
@ -161,7 +164,7 @@ public class BoxingElimination {
Wrapper q = wrappers.get(set.find(b)); Wrapper q = wrappers.get(set.find(b));
int c = set.union(a, b); int c = set.union(a, b);
if (c >= wrappers.size()) { if (c >= wrappers.size()) {
wrappers.addAll(Collections.nCopies(c - wrappers.size() + 1, (Wrapper) null)); wrappers.addAll(Collections.nCopies(c - wrappers.size() + 1, null));
} }
wrappers.set(c, union(p, q)); wrappers.set(c, union(p, q));
return c; return c;