From d5fac392d32b64f60b951998122d91cf25cb6b11 Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Tue, 25 Feb 2014 21:36:45 +0400 Subject: [PATCH] Adds try/catch block to model --- .../main/java/org/teavm/model/BasicBlock.java | 51 +++++++++++++++ .../org/teavm/model/BasicBlockReader.java | 2 + .../java/org/teavm/model/TryCatchBlock.java | 65 +++++++++++++++++++ .../org/teavm/model/TryCatchBlockReader.java | 32 +++++++++ 4 files changed, 150 insertions(+) create mode 100644 teavm-core/src/main/java/org/teavm/model/TryCatchBlock.java create mode 100644 teavm-core/src/main/java/org/teavm/model/TryCatchBlockReader.java diff --git a/teavm-core/src/main/java/org/teavm/model/BasicBlock.java b/teavm-core/src/main/java/org/teavm/model/BasicBlock.java index a4114da1e..238f38461 100644 --- a/teavm-core/src/main/java/org/teavm/model/BasicBlock.java +++ b/teavm-core/src/main/java/org/teavm/model/BasicBlock.java @@ -27,6 +27,7 @@ public class BasicBlock implements BasicBlockReader { private int index; private List phis = new ArrayList<>(); private List instructions = new ArrayList<>(); + List 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 immutableTryCatchBlocks = Collections.unmodifiableList(tryCatchBlocks); + + @Override + public List readTryCatchBlocks() { + return immutableTryCatchBlocks; + } + + private List safeTryCatchBlocks = new AbstractList() { + @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 getTryCatchBlocks() { + return safeTryCatchBlocks; + } } diff --git a/teavm-core/src/main/java/org/teavm/model/BasicBlockReader.java b/teavm-core/src/main/java/org/teavm/model/BasicBlockReader.java index 1cc01d8ce..7f3333fe6 100644 --- a/teavm-core/src/main/java/org/teavm/model/BasicBlockReader.java +++ b/teavm-core/src/main/java/org/teavm/model/BasicBlockReader.java @@ -34,4 +34,6 @@ public interface BasicBlockReader { void readInstruction(int index, InstructionReader reader); void readAllInstructions(InstructionReader reader); + + List readTryCatchBlocks(); } diff --git a/teavm-core/src/main/java/org/teavm/model/TryCatchBlock.java b/teavm-core/src/main/java/org/teavm/model/TryCatchBlock.java new file mode 100644 index 000000000..ec9f4cab7 --- /dev/null +++ b/teavm-core/src/main/java/org/teavm/model/TryCatchBlock.java @@ -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 + */ +public class TryCatchBlock implements TryCatchBlockReader { + Set 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 immutableProtectedBLocks = Collections.unmodifiableSet(protectedBlocks); + + @Override + public Set readProtectedBlocks() { + return immutableProtectedBLocks; + } +} diff --git a/teavm-core/src/main/java/org/teavm/model/TryCatchBlockReader.java b/teavm-core/src/main/java/org/teavm/model/TryCatchBlockReader.java new file mode 100644 index 000000000..f9b84ca1b --- /dev/null +++ b/teavm-core/src/main/java/org/teavm/model/TryCatchBlockReader.java @@ -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 + */ +public interface TryCatchBlockReader { + Set readProtectedBlocks(); + + BasicBlockReader getHandler(); + + String getExceptionType(); + + VariableReader getExceptionVariable(); +}