Adds try/catch block to model

This commit is contained in:
konsoletyper 2014-02-25 21:36:45 +04:00
parent 753bb4b23e
commit d5fac392d3
4 changed files with 150 additions and 0 deletions

View File

@ -27,6 +27,7 @@ public class BasicBlock implements BasicBlockReader {
private int index;
private List<Phi> phis = new ArrayList<>();
private List<Instruction> instructions = new ArrayList<>();
List<TryCatchBlock> tryCatchBlocks = new ArrayList<>();
BasicBlock(Program program, int index) {
this.program = program;
@ -181,4 +182,54 @@ public class BasicBlock implements BasicBlockReader {
insn.acceptVisitor(visitor);
}
}
private List<TryCatchBlock> immutableTryCatchBlocks = Collections.unmodifiableList(tryCatchBlocks);
@Override
public List<TryCatchBlock> readTryCatchBlocks() {
return immutableTryCatchBlocks;
}
private List<TryCatchBlock> safeTryCatchBlocks = new AbstractList<TryCatchBlock>() {
@Override public TryCatchBlock get(int index) {
return tryCatchBlocks.get(index);
}
@Override public int size() {
return tryCatchBlocks.size();
}
@Override public void add(int index, TryCatchBlock element) {
if (!element.protectedBlocks.add(BasicBlock.this)) {
throw new IllegalStateException("This try/catch block is already added to basic block");
}
tryCatchBlocks.add(index, element);
}
@Override public TryCatchBlock remove(int index) {
TryCatchBlock tryCatch = tryCatchBlocks.remove(index);
tryCatch.protectedBlocks.remove(BasicBlock.this);
return tryCatch;
};
@Override public TryCatchBlock set(int index, TryCatchBlock element) {
TryCatchBlock oldTryCatch = tryCatchBlocks.get(index);
if (oldTryCatch == element) {
return oldTryCatch;
}
if (element.protectedBlocks.contains(BasicBlock.this)) {
throw new IllegalStateException("This try/catch block is already added to basic block");
}
oldTryCatch.protectedBlocks.remove(BasicBlock.this);
element.protectedBlocks.add(BasicBlock.this);
tryCatchBlocks.set(index, element);
return oldTryCatch;
};
@Override public void clear() {
for (TryCatchBlock tryCatch : tryCatchBlocks) {
tryCatch.protectedBlocks.remove(BasicBlock.this);
}
tryCatchBlocks.clear();
};
};
public List<TryCatchBlock> getTryCatchBlocks() {
return safeTryCatchBlocks;
}
}

View File

@ -34,4 +34,6 @@ public interface BasicBlockReader {
void readInstruction(int index, InstructionReader reader);
void readAllInstructions(InstructionReader reader);
List<? extends TryCatchBlockReader> readTryCatchBlocks();
}

View File

@ -0,0 +1,65 @@
/*
* 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.model;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/**
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
public class TryCatchBlock implements TryCatchBlockReader {
Set<BasicBlock> protectedBlocks = new HashSet<>();
private BasicBlock handler;
private String exceptionType;
private Variable exceptionVariable;
@Override
public BasicBlock getHandler() {
return handler;
}
public void setHandler(BasicBlock handler) {
this.handler = handler;
}
@Override
public String getExceptionType() {
return exceptionType;
}
public void setExceptionType(String exceptionType) {
this.exceptionType = exceptionType;
}
@Override
public Variable getExceptionVariable() {
return exceptionVariable;
}
public void setExceptionVariable(Variable exceptionVariable) {
this.exceptionVariable = exceptionVariable;
}
private Set<BasicBlock> immutableProtectedBLocks = Collections.unmodifiableSet(protectedBlocks);
@Override
public Set<BasicBlock> readProtectedBlocks() {
return immutableProtectedBLocks;
}
}

View File

@ -0,0 +1,32 @@
/*
* 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.model;
import java.util.Set;
/**
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
public interface TryCatchBlockReader {
Set<? extends BasicBlockReader> readProtectedBlocks();
BasicBlockReader getHandler();
String getExceptionType();
VariableReader getExceptionVariable();
}