From 161eb2b9c08fda70d8a395c5f0dc4ecdcc00e758 Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Wed, 22 Jan 2014 13:02:27 +0400 Subject: [PATCH] Removes "representative" property of variables in SSA form --- .../teavm/javascript/JavascriptBuilder.java | 6 +- .../main/java/org/teavm/model/Program.java | 1 - .../main/java/org/teavm/model/Variable.java | 9 -- .../model/util/InstructionStringifier.java | 132 +++++++++--------- .../org/teavm/model/util/ListingBuilder.java | 6 +- .../org/teavm/model/util/PhiEliminator.java | 24 ---- .../org/teavm/parsing/SSATransformer.java | 5 - 7 files changed, 73 insertions(+), 110 deletions(-) delete mode 100644 teavm-core/src/main/java/org/teavm/model/util/PhiEliminator.java diff --git a/teavm-core/src/main/java/org/teavm/javascript/JavascriptBuilder.java b/teavm-core/src/main/java/org/teavm/javascript/JavascriptBuilder.java index 92780ffea..2b7ba0135 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/JavascriptBuilder.java +++ b/teavm-core/src/main/java/org/teavm/javascript/JavascriptBuilder.java @@ -109,6 +109,9 @@ public class JavascriptBuilder { ValueType.arrayOf(ValueType.CHARACTER), ValueType.VOID))); dependencyChecker.checkDependencies(); ListableClassHolderSource classSet = dependencyChecker.cutUnachievableClasses(); + Decompiler decompiler = new Decompiler(classSet, classLoader); + ClassSetOptimizer optimizer = new ClassSetOptimizer(); + optimizer.optimizeAll(classSet); if (bytecodeLogging) { try { logBytecode(new PrintWriter(new OutputStreamWriter(logStream, "UTF-8")), classSet); @@ -116,9 +119,6 @@ public class JavascriptBuilder { // Just don't do anything } } - Decompiler decompiler = new Decompiler(classSet, classLoader); - ClassSetOptimizer optimizer = new ClassSetOptimizer(); - optimizer.optimizeAll(classSet); renderer.renderRuntime(); List clsNodes = decompiler.decompile(classSet.getClassNames()); for (ClassNode clsNode : clsNodes) { diff --git a/teavm-core/src/main/java/org/teavm/model/Program.java b/teavm-core/src/main/java/org/teavm/model/Program.java index cf7715463..470012c4e 100644 --- a/teavm-core/src/main/java/org/teavm/model/Program.java +++ b/teavm-core/src/main/java/org/teavm/model/Program.java @@ -37,7 +37,6 @@ public class Program { public Variable createVariable() { Variable variable = new Variable(this); variable.setIndex(variables.size()); - variable.setRepresentative(variable.getIndex()); variables.add(variable); return variable; } diff --git a/teavm-core/src/main/java/org/teavm/model/Variable.java b/teavm-core/src/main/java/org/teavm/model/Variable.java index 9c97b42a6..d363aed42 100644 --- a/teavm-core/src/main/java/org/teavm/model/Variable.java +++ b/teavm-core/src/main/java/org/teavm/model/Variable.java @@ -22,7 +22,6 @@ package org.teavm.model; public class Variable { private Program program; private int index; - private int representative; Variable(Program program) { this.program = program; @@ -43,12 +42,4 @@ public class Variable { void setProgram(Program program) { this.program = program; } - - public int getRepresentative() { - return representative; - } - - public void setRepresentative(int representative) { - this.representative = representative; - } } diff --git a/teavm-core/src/main/java/org/teavm/model/util/InstructionStringifier.java b/teavm-core/src/main/java/org/teavm/model/util/InstructionStringifier.java index 3e480948b..1767315ab 100644 --- a/teavm-core/src/main/java/org/teavm/model/util/InstructionStringifier.java +++ b/teavm-core/src/main/java/org/teavm/model/util/InstructionStringifier.java @@ -30,10 +30,6 @@ public class InstructionStringifier implements InstructionVisitor { this.sb = sb; } - private StringBuilder appendVar(Variable var) { - return sb.append('@').append(var.getIndex()).append(":").append(var.getRepresentative()); - } - @Override public void visit(EmptyInstruction insn) { sb.append("nop"); @@ -41,43 +37,49 @@ public class InstructionStringifier implements InstructionVisitor { @Override public void visit(ClassConstantInstruction insn) { - appendVar(insn.getReceiver()).append(" := classOf ").append(insn.getConstant()); + sb.append("@").append(insn.getReceiver().getIndex()).append(" := classOf ") + .append(insn.getConstant()); } @Override public void visit(NullConstantInstruction insn) { - appendVar(insn.getReceiver()).append(" := null"); + sb.append("@").append(insn.getReceiver().getIndex()).append(" := null"); } @Override public void visit(IntegerConstantInstruction insn) { - appendVar(insn.getReceiver()).append(" := ").append(insn.getConstant()); + sb.append("@").append(insn.getReceiver().getIndex()).append(" := ") + .append(insn.getConstant()); } @Override public void visit(LongConstantInstruction insn) { - appendVar(insn.getReceiver()).append(" := ").append(insn.getConstant()); + sb.append("@").append(insn.getReceiver().getIndex()).append(" := ") + .append(insn.getConstant()); } @Override public void visit(FloatConstantInstruction insn) { - appendVar(insn.getReceiver()).append(" := ").append(insn.getConstant()); + sb.append("@").append(insn.getReceiver().getIndex()).append(" := ") + .append(insn.getConstant()); } @Override public void visit(DoubleConstantInstruction insn) { - appendVar(insn.getReceiver()).append(" := ").append(insn.getConstant()); + sb.append("@").append(insn.getReceiver().getIndex()).append(" := ") + .append(insn.getConstant()); } @Override public void visit(StringConstantInstruction insn) { - appendVar(insn.getReceiver()).append(" := '").append(insn.getConstant()).append("'"); + sb.append("@").append(insn.getReceiver().getIndex()).append(" := '") + .append(insn.getConstant()).append("'"); } @Override public void visit(BinaryInstruction insn) { - appendVar(insn.getReceiver()).append(" := "); - appendVar(insn.getFirstOperand()).append(" "); + sb.append("@").append(insn.getReceiver().getIndex()).append(" := @") + .append(insn.getFirstOperand().getIndex()).append(" "); switch (insn.getOperation()) { case ADD: sb.append("+"); @@ -116,26 +118,24 @@ public class InstructionStringifier implements InstructionVisitor { sb.append("^"); break; } - sb.append(' '); - appendVar(insn.getSecondOperand()); + sb.append(" @").append(insn.getSecondOperand().getIndex()); } @Override public void visit(NegateInstruction insn) { - appendVar(insn.getReceiver()).append(" := -"); - appendVar(insn.getOperand()); + sb.append("@").append(insn.getReceiver().getIndex()).append(" := -") + .append(" @").append(insn.getOperand().getIndex()); } @Override public void visit(AssignInstruction insn) { - appendVar(insn.getReceiver()).append(" := "); - appendVar(insn.getAssignee()); + sb.append("@").append(insn.getReceiver().getIndex()).append(" := @") + .append(insn.getAssignee().getIndex()); } @Override public void visit(BranchingInstruction insn) { - sb.append("if "); - appendVar(insn.getOperand()).append(" "); + sb.append("if @").append(insn.getOperand().getIndex()).append(" "); switch (insn.getCondition()) { case EQUAL: sb.append("== 0"); @@ -168,8 +168,7 @@ public class InstructionStringifier implements InstructionVisitor { @Override public void visit(BinaryBranchingInstruction insn) { - sb.append("if "); - appendVar(insn.getFirstOperand()).append(" "); + sb.append("if @").append(insn.getFirstOperand().getIndex()).append(" "); switch (insn.getCondition()) { case EQUAL: case REFERENCE_EQUAL: @@ -180,7 +179,8 @@ public class InstructionStringifier implements InstructionVisitor { sb.append("!="); break; } - appendVar(insn.getSecondOperand()).append(" then goto $").append(insn.getConsequent().getIndex()) + sb.append("@").append(insn.getSecondOperand().getIndex()) + .append(" then goto $").append(insn.getConsequent().getIndex()) .append(" else goto $").append(insn.getAlternative().getIndex()); } @@ -191,15 +191,15 @@ public class InstructionStringifier implements InstructionVisitor { @Override public void visit(SwitchInstruction insn) { - sb.append("switch "); - appendVar(insn.getCondition()).append(" "); + sb.append("switch @").append(insn.getCondition().getIndex()).append(" "); List entries = insn.getEntries(); for (int i = 0; i < entries.size(); ++i) { if (i > 0) { sb.append("; "); } SwitchTableEntry entry = entries.get(i); - sb.append("case ").append(entry.getCondition()).append(": goto $").append(entry.getTarget()); + sb.append("case ").append(entry.getCondition()).append(": goto $") + .append(entry.getTarget()); } sb.append(", default: goto $").append(insn.getDefaultTarget()); } @@ -208,46 +208,48 @@ public class InstructionStringifier implements InstructionVisitor { public void visit(ExitInstruction insn) { sb.append("return"); if (insn.getValueToReturn() != null) { - sb.append(" "); - appendVar(insn.getValueToReturn()); + sb.append(" @").append(insn.getValueToReturn().getIndex()); } } @Override public void visit(RaiseInstruction insn) { - sb.append("throw "); - appendVar(insn.getException()); + sb.append("throw @").append(insn.getException().getIndex()); } @Override public void visit(ConstructArrayInstruction insn) { - appendVar(insn.getReceiver()).append(" = new ").append(insn.getItemType()).append("["); - appendVar(insn.getSize()).append(']'); + sb.append("@").append(insn.getReceiver().getIndex()).append(" = new ") + .append(insn.getItemType()).append("[@").append(insn.getSize().getIndex()) + .append(']'); } @Override public void visit(ConstructInstruction insn) { - appendVar(insn.getReceiver()).append(" = new ").append(insn.getType()).append("()"); + sb.append("@").append(insn.getReceiver().getIndex()).append(" = new ") + .append(insn.getType()).append("()"); } @Override public void visit(ConstructMultiArrayInstruction insn) { - appendVar(insn.getReceiver()).append(" = new ").append(insn.getItemType()).append("["); + sb.append("@").append(insn.getReceiver().getIndex()).append(" = new ") + .append(insn.getItemType()).append("["); List dimensions = insn.getDimensions(); for (int i = 0; i < dimensions.size(); ++i) { if (i > 0) { sb.append(", "); } - appendVar(dimensions.get(i)); + Variable dimension = dimensions.get(i); + sb.append("@").append(dimension.getIndex()); } sb.append("]"); } @Override public void visit(GetFieldInstruction insn) { - appendVar(insn.getReceiver()).append(" := "); + sb.append("@").append(insn.getReceiver().getIndex()).append(" := "); if (insn.getInstance() != null) { - appendVar(insn.getInstance()); + sb.append("@").append(insn.getInstance().getIndex()); } else { sb.append(insn.getField().getClassName()); } @@ -257,35 +259,34 @@ public class InstructionStringifier implements InstructionVisitor { @Override public void visit(PutFieldInstruction insn) { if (insn.getInstance() != null) { - appendVar(insn.getInstance()); + sb.append("@").append(insn.getInstance().getIndex()); } else { sb.append(insn.getField().getClassName()); } - sb.append(".").append(insn.getField()).append(" := "); - appendVar(insn.getValue()); + sb.append(".").append(insn.getField()).append(" := @").append(insn.getValue().getIndex()); } @Override public void visit(GetElementInstruction insn) { - appendVar(insn.getReceiver()).append(" := "); - appendVar(insn.getArray()).append("["); - appendVar(insn.getIndex()).append("]"); + sb.append("@").append(insn.getReceiver().getIndex()).append(" := @") + .append(insn.getArray().getIndex()).append("[@") + .append(insn.getIndex().getIndex()).append("]"); } @Override public void visit(PutElementInstruction insn) { - appendVar(insn.getArray()).append("["); - appendVar(insn.getIndex()).append("] := "); - appendVar(insn.getValue()); + sb.append("@").append(insn.getArray().getIndex()).append("[@") + .append(insn.getIndex().getIndex()).append("] := @") + .append(insn.getValue().getIndex()); } @Override public void visit(InvokeInstruction insn) { if (insn.getReceiver() != null) { - appendVar(insn.getReceiver()).append(" := "); + sb.append("@").append(insn.getReceiver().getIndex()).append(" := "); } if (insn.getInstance() != null) { - appendVar(insn.getInstance()); + sb.append("@").append(insn.getInstance().getIndex()); } else { sb.append(insn.getMethod().getClassName()); } @@ -295,52 +296,55 @@ public class InstructionStringifier implements InstructionVisitor { if (i > 0) { sb.append(", "); } - appendVar(arguments.get(i)); + sb.append("@").append(arguments.get(i).getIndex()); } sb.append(")"); } @Override public void visit(IsInstanceInstruction insn) { - appendVar(insn.getReceiver()).append(" := "); - appendVar(insn.getValue()).append(" instanceof ").append(insn.getType()); + sb.append("@").append(insn.getReceiver().getIndex()).append(" := @") + .append(insn.getValue().getIndex()).append(" instanceof ").append(insn.getType()); } @Override public void visit(CastInstruction insn) { - appendVar(insn.getReceiver()).append(" := cast "); - appendVar(insn.getValue()).append(" to ").append(insn.getTargetType()); + sb.append("@").append(insn.getReceiver().getIndex()).append(" := cast @") + .append(insn.getValue().getIndex()).append(" to ") + .append(insn.getTargetType()); } @Override public void visit(CastNumberInstruction insn) { - appendVar(insn.getReceiver()).append(" := cast "); - appendVar(insn.getValue()).append(" from ").append(insn.getSourceType()) + sb.append("@").append(insn.getReceiver().getIndex()).append(" := cast @") + .append(insn.getValue().getIndex()) + .append(" from ").append(insn.getSourceType()) .append(" to ").append(insn.getTargetType()); } @Override public void visit(CastIntegerInstruction insn) { - appendVar(insn.getReceiver()).append(" := cast "); - appendVar(insn.getValue()).append(" from INT to ").append(insn.getTargetType()); + sb.append("@").append(insn.getReceiver().getIndex()).append(" := cast @") + .append(insn.getValue().getIndex()) + .append(" from INT to ").append(insn.getTargetType()); } @Override public void visit(UnwrapArrayInstruction insn) { - appendVar(insn.getReceiver()).append(" := "); - appendVar(insn.getArray()).append(".data"); + sb.append("@").append(insn.getReceiver().getIndex()).append(" := @") + .append(insn.getArray()).append(".data"); } @Override public void visit(ArrayLengthInstruction insn) { - appendVar(insn.getReceiver()).append(" := "); - appendVar(insn.getArray()).append(".length"); + sb.append("@").append(insn.getReceiver().getIndex()).append(" := @") + .append(insn.getArray().getIndex()).append(".length"); } @Override public void visit(CloneArrayInstruction insn) { - appendVar(insn.getReceiver()).append(" := "); - appendVar(insn.getArray()).append(".clone()"); + sb.append("@").append(insn.getReceiver().getIndex()).append("@") + .append(insn.getArray().getIndex()).append(".clone()"); } @Override diff --git a/teavm-core/src/main/java/org/teavm/model/util/ListingBuilder.java b/teavm-core/src/main/java/org/teavm/model/util/ListingBuilder.java index 233920900..c11872c09 100644 --- a/teavm-core/src/main/java/org/teavm/model/util/ListingBuilder.java +++ b/teavm-core/src/main/java/org/teavm/model/util/ListingBuilder.java @@ -31,16 +31,14 @@ public class ListingBuilder { sb.append(prefix).append("$").append(i).append(":\n"); for (Phi phi : block.getPhis()) { sb.append(prefix).append(" "); - sb.append("@").append(phi.getReceiver().getIndex()).append(":") - .append(phi.getReceiver().getRepresentative()).append(" := "); + sb.append("@").append(phi.getReceiver().getIndex()).append(" := "); List incomings = phi.getIncomings(); for (int j = 0; j < incomings.size(); ++j) { if (j > 0) { sb.append(", "); } Incoming incoming = incomings.get(j); - sb.append("@").append(incoming.getValue().getIndex()).append(":") - .append(incoming.getValue().getRepresentative()).append(" from ") + sb.append("@").append(incoming.getValue().getIndex()).append(" from ") .append("$").append(incoming.getSource().getIndex()); } sb.append("\n"); diff --git a/teavm-core/src/main/java/org/teavm/model/util/PhiEliminator.java b/teavm-core/src/main/java/org/teavm/model/util/PhiEliminator.java deleted file mode 100644 index c578fec9f..000000000 --- a/teavm-core/src/main/java/org/teavm/model/util/PhiEliminator.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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.util; - -/** - * - * @author Alexey Andreev - */ -public class PhiEliminator { - -} diff --git a/teavm-core/src/main/java/org/teavm/parsing/SSATransformer.java b/teavm-core/src/main/java/org/teavm/parsing/SSATransformer.java index ad9343b3e..0a933bab5 100644 --- a/teavm-core/src/main/java/org/teavm/parsing/SSATransformer.java +++ b/teavm-core/src/main/java/org/teavm/parsing/SSATransformer.java @@ -77,9 +77,6 @@ public class SSATransformer { } } } - for (int i = 0; i <= arguments.length; ++i) { - program.variableAt(i).setRepresentative(i); - } arguments = null; } @@ -126,7 +123,6 @@ public class SSATransformer { for (Phi phi : currentBlock.getPhis()) { Variable var = program.createVariable(); variableMap[phi.getReceiver().getIndex()] = var; - var.setRepresentative(phi.getReceiver().getIndex()); phi.setReceiver(var); } for (Instruction insn : currentBlock.getInstructions()) { @@ -185,7 +181,6 @@ public class SSATransformer { private Variable define(Variable var) { Variable result = program.createVariable(); - result.setRepresentative(var.getIndex()); variableMap[var.getIndex()] = result; return result; }