From 0e3fb1f3d2251b0216a8d5d1681203bce7889780 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 27 Mar 2015 15:42:47 +0400 Subject: [PATCH] Modify LivenessAnaylizer to not rely on dominator tree. Fix possible name clash between virtual methods and fields. Remove tests incompatible between JDK7 and JDK8 --- .../teavm/codegen/DefaultAliasProvider.java | 15 ++++++-- .../teavm/codegen/MinifyingAliasProvider.java | 15 ++++++-- .../teavm/model/util/LivenessAnalyzer.java | 35 +------------------ .../classlib/java/nio/charset/UTF8Test.java | 19 ---------- 4 files changed, 27 insertions(+), 57 deletions(-) diff --git a/teavm-core/src/main/java/org/teavm/codegen/DefaultAliasProvider.java b/teavm-core/src/main/java/org/teavm/codegen/DefaultAliasProvider.java index 0cc90a5d5..408ed9538 100644 --- a/teavm-core/src/main/java/org/teavm/codegen/DefaultAliasProvider.java +++ b/teavm-core/src/main/java/org/teavm/codegen/DefaultAliasProvider.java @@ -15,6 +15,8 @@ */ package org.teavm.codegen; +import java.util.HashSet; +import java.util.Set; import org.teavm.model.FieldReference; import org.teavm.model.MethodDescriptor; import org.teavm.model.MethodReference; @@ -26,6 +28,7 @@ import org.teavm.model.MethodReference; public class DefaultAliasProvider implements AliasProvider { private int lastSuffix; private int lastVirtualSuffix; + private Set usedAliases = new HashSet<>(); @Override public String getAlias(String cls) { @@ -58,7 +61,11 @@ public class DefaultAliasProvider implements AliasProvider { } else if (alias.equals("")) { alias = "$clinit"; } - return alias + lastVirtualSuffix++; + String result; + do { + result = alias + lastVirtualSuffix++; + } while (!usedAliases.add(result)); + return result; } @Override @@ -74,7 +81,11 @@ public class DefaultAliasProvider implements AliasProvider { @Override public String getAlias(FieldReference field) { - return field.getFieldName() + (lastSuffix++); + String result; + do { + result = field.getFieldName() + lastSuffix++; + } while (!usedAliases.add(result)); + return result; } @Override diff --git a/teavm-core/src/main/java/org/teavm/codegen/MinifyingAliasProvider.java b/teavm-core/src/main/java/org/teavm/codegen/MinifyingAliasProvider.java index a51ce9a3a..b7eaae831 100644 --- a/teavm-core/src/main/java/org/teavm/codegen/MinifyingAliasProvider.java +++ b/teavm-core/src/main/java/org/teavm/codegen/MinifyingAliasProvider.java @@ -15,6 +15,8 @@ */ package org.teavm.codegen; +import java.util.HashSet; +import java.util.Set; import org.teavm.model.FieldReference; import org.teavm.model.MethodDescriptor; import org.teavm.model.MethodReference; @@ -29,10 +31,15 @@ public class MinifyingAliasProvider implements AliasProvider { private static String startVirtualLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; private int lastSuffix; private int lastVirtual; + private Set usedAliases = new HashSet<>(); @Override public String getAlias(FieldReference field) { - return getNewAlias(lastVirtual++, startVirtualLetters); + String result; + do { + result = getNewAlias(lastVirtual++, startVirtualLetters); + } while (!usedAliases.add(result)); + return result; } @Override @@ -42,7 +49,11 @@ public class MinifyingAliasProvider implements AliasProvider { @Override public String getAlias(MethodDescriptor method) { - return getNewAlias(lastVirtual++, startVirtualLetters); + String result; + do { + result = getNewAlias(lastVirtual++, startVirtualLetters); + } while (!usedAliases.add(result)); + return result; } @Override diff --git a/teavm-core/src/main/java/org/teavm/model/util/LivenessAnalyzer.java b/teavm-core/src/main/java/org/teavm/model/util/LivenessAnalyzer.java index d0bac2296..cfb3ca677 100644 --- a/teavm-core/src/main/java/org/teavm/model/util/LivenessAnalyzer.java +++ b/teavm-core/src/main/java/org/teavm/model/util/LivenessAnalyzer.java @@ -19,7 +19,6 @@ import java.util.ArrayDeque; import java.util.BitSet; import java.util.Deque; import org.teavm.common.Graph; -import org.teavm.common.GraphUtils; import org.teavm.model.*; /** @@ -28,8 +27,6 @@ import org.teavm.model.*; */ public class LivenessAnalyzer { private BitSet[] liveVars; - private int[] domLeft; - private int[] domRight; public boolean liveIn(int block, int var) { return liveVars[block].get(var); @@ -41,7 +38,6 @@ public class LivenessAnalyzer { public void analyze(Program program) { Graph cfg = ProgramUtils.buildControlFlowGraph(program); - computeDomLeftRight(GraphUtils.buildDominatorGraph(GraphUtils.buildDominatorTree(cfg), cfg.size())); liveVars = new BitSet[cfg.size()]; for (int i = 0; i < liveVars.length; ++i) { liveVars[i] = new BitSet(program.basicBlockCount()); @@ -84,7 +80,7 @@ public class LivenessAnalyzer { while (!stack.isEmpty()) { Task task = stack.pop(); - if (liveVars[task.block].get(task.var) || !dominates(definitions[task.var], task.block)) { + if (liveVars[task.block].get(task.var) || definitions[task.var] == task.block) { continue; } liveVars[task.block].set(task.var, true); @@ -97,35 +93,6 @@ public class LivenessAnalyzer { } } - private void computeDomLeftRight(Graph domGraph) { - domLeft = new int[domGraph.size()]; - domRight = new int[domGraph.size()]; - int index = 1; - int[] stack = new int[domGraph.size() * 2]; - int top = 0; - for (int i = domGraph.size() - 1; i >= 0; --i) { - if (domGraph.incomingEdgesCount(i) == 0) { - stack[top++] = i; - } - } - while (top > 0) { - int v = stack[--top]; - if (domLeft[v] == 0) { - domLeft[v] = index++; - stack[top++] = v; - for (int succ : domGraph.outgoingEdges(v)) { - stack[top++] = succ; - } - } else if (domRight[v] == 0) { - domRight[v] = index++; - } - } - } - - private boolean dominates(int a, int b) { - return domLeft[a] < domLeft[b] && domRight[a] > domRight[b]; - } - private static class Task { int block; int var; diff --git a/teavm-tests/src/test/java/org/teavm/classlib/java/nio/charset/UTF8Test.java b/teavm-tests/src/test/java/org/teavm/classlib/java/nio/charset/UTF8Test.java index f56636e22..82925b742 100644 --- a/teavm-tests/src/test/java/org/teavm/classlib/java/nio/charset/UTF8Test.java +++ b/teavm-tests/src/test/java/org/teavm/classlib/java/nio/charset/UTF8Test.java @@ -93,25 +93,6 @@ public class UTF8Test { assertEquals("a\uFFFDbb", new String(result)); } - @Test - public void replaceDecodedSurrogate() { - Charset charset = Charset.forName("UTF-8"); - CharBuffer buffer = charset.decode(ByteBuffer.wrap(new byte[] { 97, (byte)0xED, (byte)0xA0, (byte)0x80, 98 })); - char[] result = new char[buffer.remaining()]; - buffer.get(result); - assertEquals("a\uFFFDb", new String(result)); - } - - @Test - public void replaceDecodedSurrogatePair() { - Charset charset = Charset.forName("UTF-8"); - CharBuffer buffer = charset.decode(ByteBuffer.wrap(new byte[] { 97, (byte)0xED, (byte)0xA0, (byte)0x80, - (byte)0xED, (byte)0xBF, (byte)0xBF, 98 })); - char[] result = new char[buffer.remaining()]; - buffer.get(result); - assertEquals("a\uFFFD\uFFFDb", new String(result)); - } - @Test public void decodeLongUTF8ByteArray() throws UnsupportedEncodingException { byte[] bytes = new byte[16384];