Removes "representative" property of variables in SSA form

This commit is contained in:
konsoletyper 2014-01-22 13:02:27 +04:00
parent d704e503ee
commit 161eb2b9c0
7 changed files with 73 additions and 110 deletions

View File

@ -109,6 +109,9 @@ public class JavascriptBuilder {
ValueType.arrayOf(ValueType.CHARACTER), ValueType.VOID))); ValueType.arrayOf(ValueType.CHARACTER), ValueType.VOID)));
dependencyChecker.checkDependencies(); dependencyChecker.checkDependencies();
ListableClassHolderSource classSet = dependencyChecker.cutUnachievableClasses(); ListableClassHolderSource classSet = dependencyChecker.cutUnachievableClasses();
Decompiler decompiler = new Decompiler(classSet, classLoader);
ClassSetOptimizer optimizer = new ClassSetOptimizer();
optimizer.optimizeAll(classSet);
if (bytecodeLogging) { if (bytecodeLogging) {
try { try {
logBytecode(new PrintWriter(new OutputStreamWriter(logStream, "UTF-8")), classSet); logBytecode(new PrintWriter(new OutputStreamWriter(logStream, "UTF-8")), classSet);
@ -116,9 +119,6 @@ public class JavascriptBuilder {
// Just don't do anything // Just don't do anything
} }
} }
Decompiler decompiler = new Decompiler(classSet, classLoader);
ClassSetOptimizer optimizer = new ClassSetOptimizer();
optimizer.optimizeAll(classSet);
renderer.renderRuntime(); renderer.renderRuntime();
List<ClassNode> clsNodes = decompiler.decompile(classSet.getClassNames()); List<ClassNode> clsNodes = decompiler.decompile(classSet.getClassNames());
for (ClassNode clsNode : clsNodes) { for (ClassNode clsNode : clsNodes) {

View File

@ -37,7 +37,6 @@ public class Program {
public Variable createVariable() { public Variable createVariable() {
Variable variable = new Variable(this); Variable variable = new Variable(this);
variable.setIndex(variables.size()); variable.setIndex(variables.size());
variable.setRepresentative(variable.getIndex());
variables.add(variable); variables.add(variable);
return variable; return variable;
} }

View File

@ -22,7 +22,6 @@ package org.teavm.model;
public class Variable { public class Variable {
private Program program; private Program program;
private int index; private int index;
private int representative;
Variable(Program program) { Variable(Program program) {
this.program = program; this.program = program;
@ -43,12 +42,4 @@ public class Variable {
void setProgram(Program program) { void setProgram(Program program) {
this.program = program; this.program = program;
} }
public int getRepresentative() {
return representative;
}
public void setRepresentative(int representative) {
this.representative = representative;
}
} }

View File

@ -30,10 +30,6 @@ public class InstructionStringifier implements InstructionVisitor {
this.sb = sb; this.sb = sb;
} }
private StringBuilder appendVar(Variable var) {
return sb.append('@').append(var.getIndex()).append(":").append(var.getRepresentative());
}
@Override @Override
public void visit(EmptyInstruction insn) { public void visit(EmptyInstruction insn) {
sb.append("nop"); sb.append("nop");
@ -41,43 +37,49 @@ public class InstructionStringifier implements InstructionVisitor {
@Override @Override
public void visit(ClassConstantInstruction insn) { 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 @Override
public void visit(NullConstantInstruction insn) { public void visit(NullConstantInstruction insn) {
appendVar(insn.getReceiver()).append(" := null"); sb.append("@").append(insn.getReceiver().getIndex()).append(" := null");
} }
@Override @Override
public void visit(IntegerConstantInstruction insn) { public void visit(IntegerConstantInstruction insn) {
appendVar(insn.getReceiver()).append(" := ").append(insn.getConstant()); sb.append("@").append(insn.getReceiver().getIndex()).append(" := ")
.append(insn.getConstant());
} }
@Override @Override
public void visit(LongConstantInstruction insn) { public void visit(LongConstantInstruction insn) {
appendVar(insn.getReceiver()).append(" := ").append(insn.getConstant()); sb.append("@").append(insn.getReceiver().getIndex()).append(" := ")
.append(insn.getConstant());
} }
@Override @Override
public void visit(FloatConstantInstruction insn) { public void visit(FloatConstantInstruction insn) {
appendVar(insn.getReceiver()).append(" := ").append(insn.getConstant()); sb.append("@").append(insn.getReceiver().getIndex()).append(" := ")
.append(insn.getConstant());
} }
@Override @Override
public void visit(DoubleConstantInstruction insn) { public void visit(DoubleConstantInstruction insn) {
appendVar(insn.getReceiver()).append(" := ").append(insn.getConstant()); sb.append("@").append(insn.getReceiver().getIndex()).append(" := ")
.append(insn.getConstant());
} }
@Override @Override
public void visit(StringConstantInstruction insn) { 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 @Override
public void visit(BinaryInstruction insn) { public void visit(BinaryInstruction insn) {
appendVar(insn.getReceiver()).append(" := "); sb.append("@").append(insn.getReceiver().getIndex()).append(" := @")
appendVar(insn.getFirstOperand()).append(" "); .append(insn.getFirstOperand().getIndex()).append(" ");
switch (insn.getOperation()) { switch (insn.getOperation()) {
case ADD: case ADD:
sb.append("+"); sb.append("+");
@ -116,26 +118,24 @@ public class InstructionStringifier implements InstructionVisitor {
sb.append("^"); sb.append("^");
break; break;
} }
sb.append(' '); sb.append(" @").append(insn.getSecondOperand().getIndex());
appendVar(insn.getSecondOperand());
} }
@Override @Override
public void visit(NegateInstruction insn) { public void visit(NegateInstruction insn) {
appendVar(insn.getReceiver()).append(" := -"); sb.append("@").append(insn.getReceiver().getIndex()).append(" := -")
appendVar(insn.getOperand()); .append(" @").append(insn.getOperand().getIndex());
} }
@Override @Override
public void visit(AssignInstruction insn) { public void visit(AssignInstruction insn) {
appendVar(insn.getReceiver()).append(" := "); sb.append("@").append(insn.getReceiver().getIndex()).append(" := @")
appendVar(insn.getAssignee()); .append(insn.getAssignee().getIndex());
} }
@Override @Override
public void visit(BranchingInstruction insn) { public void visit(BranchingInstruction insn) {
sb.append("if "); sb.append("if @").append(insn.getOperand().getIndex()).append(" ");
appendVar(insn.getOperand()).append(" ");
switch (insn.getCondition()) { switch (insn.getCondition()) {
case EQUAL: case EQUAL:
sb.append("== 0"); sb.append("== 0");
@ -168,8 +168,7 @@ public class InstructionStringifier implements InstructionVisitor {
@Override @Override
public void visit(BinaryBranchingInstruction insn) { public void visit(BinaryBranchingInstruction insn) {
sb.append("if "); sb.append("if @").append(insn.getFirstOperand().getIndex()).append(" ");
appendVar(insn.getFirstOperand()).append(" ");
switch (insn.getCondition()) { switch (insn.getCondition()) {
case EQUAL: case EQUAL:
case REFERENCE_EQUAL: case REFERENCE_EQUAL:
@ -180,7 +179,8 @@ public class InstructionStringifier implements InstructionVisitor {
sb.append("!="); sb.append("!=");
break; 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()); .append(" else goto $").append(insn.getAlternative().getIndex());
} }
@ -191,15 +191,15 @@ public class InstructionStringifier implements InstructionVisitor {
@Override @Override
public void visit(SwitchInstruction insn) { public void visit(SwitchInstruction insn) {
sb.append("switch "); sb.append("switch @").append(insn.getCondition().getIndex()).append(" ");
appendVar(insn.getCondition()).append(" ");
List<SwitchTableEntry> entries = insn.getEntries(); List<SwitchTableEntry> entries = insn.getEntries();
for (int i = 0; i < entries.size(); ++i) { for (int i = 0; i < entries.size(); ++i) {
if (i > 0) { if (i > 0) {
sb.append("; "); sb.append("; ");
} }
SwitchTableEntry entry = entries.get(i); 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()); sb.append(", default: goto $").append(insn.getDefaultTarget());
} }
@ -208,46 +208,48 @@ public class InstructionStringifier implements InstructionVisitor {
public void visit(ExitInstruction insn) { public void visit(ExitInstruction insn) {
sb.append("return"); sb.append("return");
if (insn.getValueToReturn() != null) { if (insn.getValueToReturn() != null) {
sb.append(" "); sb.append(" @").append(insn.getValueToReturn().getIndex());
appendVar(insn.getValueToReturn());
} }
} }
@Override @Override
public void visit(RaiseInstruction insn) { public void visit(RaiseInstruction insn) {
sb.append("throw "); sb.append("throw @").append(insn.getException().getIndex());
appendVar(insn.getException());
} }
@Override @Override
public void visit(ConstructArrayInstruction insn) { public void visit(ConstructArrayInstruction insn) {
appendVar(insn.getReceiver()).append(" = new ").append(insn.getItemType()).append("["); sb.append("@").append(insn.getReceiver().getIndex()).append(" = new ")
appendVar(insn.getSize()).append(']'); .append(insn.getItemType()).append("[@").append(insn.getSize().getIndex())
.append(']');
} }
@Override @Override
public void visit(ConstructInstruction insn) { 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 @Override
public void visit(ConstructMultiArrayInstruction insn) { 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<Variable> dimensions = insn.getDimensions(); List<Variable> dimensions = insn.getDimensions();
for (int i = 0; i < dimensions.size(); ++i) { for (int i = 0; i < dimensions.size(); ++i) {
if (i > 0) { if (i > 0) {
sb.append(", "); sb.append(", ");
} }
appendVar(dimensions.get(i)); Variable dimension = dimensions.get(i);
sb.append("@").append(dimension.getIndex());
} }
sb.append("]"); sb.append("]");
} }
@Override @Override
public void visit(GetFieldInstruction insn) { public void visit(GetFieldInstruction insn) {
appendVar(insn.getReceiver()).append(" := "); sb.append("@").append(insn.getReceiver().getIndex()).append(" := ");
if (insn.getInstance() != null) { if (insn.getInstance() != null) {
appendVar(insn.getInstance()); sb.append("@").append(insn.getInstance().getIndex());
} else { } else {
sb.append(insn.getField().getClassName()); sb.append(insn.getField().getClassName());
} }
@ -257,35 +259,34 @@ public class InstructionStringifier implements InstructionVisitor {
@Override @Override
public void visit(PutFieldInstruction insn) { public void visit(PutFieldInstruction insn) {
if (insn.getInstance() != null) { if (insn.getInstance() != null) {
appendVar(insn.getInstance()); sb.append("@").append(insn.getInstance().getIndex());
} else { } else {
sb.append(insn.getField().getClassName()); sb.append(insn.getField().getClassName());
} }
sb.append(".").append(insn.getField()).append(" := "); sb.append(".").append(insn.getField()).append(" := @").append(insn.getValue().getIndex());
appendVar(insn.getValue());
} }
@Override @Override
public void visit(GetElementInstruction insn) { public void visit(GetElementInstruction insn) {
appendVar(insn.getReceiver()).append(" := "); sb.append("@").append(insn.getReceiver().getIndex()).append(" := @")
appendVar(insn.getArray()).append("["); .append(insn.getArray().getIndex()).append("[@")
appendVar(insn.getIndex()).append("]"); .append(insn.getIndex().getIndex()).append("]");
} }
@Override @Override
public void visit(PutElementInstruction insn) { public void visit(PutElementInstruction insn) {
appendVar(insn.getArray()).append("["); sb.append("@").append(insn.getArray().getIndex()).append("[@")
appendVar(insn.getIndex()).append("] := "); .append(insn.getIndex().getIndex()).append("] := @")
appendVar(insn.getValue()); .append(insn.getValue().getIndex());
} }
@Override @Override
public void visit(InvokeInstruction insn) { public void visit(InvokeInstruction insn) {
if (insn.getReceiver() != null) { if (insn.getReceiver() != null) {
appendVar(insn.getReceiver()).append(" := "); sb.append("@").append(insn.getReceiver().getIndex()).append(" := ");
} }
if (insn.getInstance() != null) { if (insn.getInstance() != null) {
appendVar(insn.getInstance()); sb.append("@").append(insn.getInstance().getIndex());
} else { } else {
sb.append(insn.getMethod().getClassName()); sb.append(insn.getMethod().getClassName());
} }
@ -295,52 +296,55 @@ public class InstructionStringifier implements InstructionVisitor {
if (i > 0) { if (i > 0) {
sb.append(", "); sb.append(", ");
} }
appendVar(arguments.get(i)); sb.append("@").append(arguments.get(i).getIndex());
} }
sb.append(")"); sb.append(")");
} }
@Override @Override
public void visit(IsInstanceInstruction insn) { public void visit(IsInstanceInstruction insn) {
appendVar(insn.getReceiver()).append(" := "); sb.append("@").append(insn.getReceiver().getIndex()).append(" := @")
appendVar(insn.getValue()).append(" instanceof ").append(insn.getType()); .append(insn.getValue().getIndex()).append(" instanceof ").append(insn.getType());
} }
@Override @Override
public void visit(CastInstruction insn) { public void visit(CastInstruction insn) {
appendVar(insn.getReceiver()).append(" := cast "); sb.append("@").append(insn.getReceiver().getIndex()).append(" := cast @")
appendVar(insn.getValue()).append(" to ").append(insn.getTargetType()); .append(insn.getValue().getIndex()).append(" to ")
.append(insn.getTargetType());
} }
@Override @Override
public void visit(CastNumberInstruction insn) { public void visit(CastNumberInstruction insn) {
appendVar(insn.getReceiver()).append(" := cast "); sb.append("@").append(insn.getReceiver().getIndex()).append(" := cast @")
appendVar(insn.getValue()).append(" from ").append(insn.getSourceType()) .append(insn.getValue().getIndex())
.append(" from ").append(insn.getSourceType())
.append(" to ").append(insn.getTargetType()); .append(" to ").append(insn.getTargetType());
} }
@Override @Override
public void visit(CastIntegerInstruction insn) { public void visit(CastIntegerInstruction insn) {
appendVar(insn.getReceiver()).append(" := cast "); sb.append("@").append(insn.getReceiver().getIndex()).append(" := cast @")
appendVar(insn.getValue()).append(" from INT to ").append(insn.getTargetType()); .append(insn.getValue().getIndex())
.append(" from INT to ").append(insn.getTargetType());
} }
@Override @Override
public void visit(UnwrapArrayInstruction insn) { public void visit(UnwrapArrayInstruction insn) {
appendVar(insn.getReceiver()).append(" := "); sb.append("@").append(insn.getReceiver().getIndex()).append(" := @")
appendVar(insn.getArray()).append(".data"); .append(insn.getArray()).append(".data");
} }
@Override @Override
public void visit(ArrayLengthInstruction insn) { public void visit(ArrayLengthInstruction insn) {
appendVar(insn.getReceiver()).append(" := "); sb.append("@").append(insn.getReceiver().getIndex()).append(" := @")
appendVar(insn.getArray()).append(".length"); .append(insn.getArray().getIndex()).append(".length");
} }
@Override @Override
public void visit(CloneArrayInstruction insn) { public void visit(CloneArrayInstruction insn) {
appendVar(insn.getReceiver()).append(" := "); sb.append("@").append(insn.getReceiver().getIndex()).append("@")
appendVar(insn.getArray()).append(".clone()"); .append(insn.getArray().getIndex()).append(".clone()");
} }
@Override @Override

View File

@ -31,16 +31,14 @@ public class ListingBuilder {
sb.append(prefix).append("$").append(i).append(":\n"); sb.append(prefix).append("$").append(i).append(":\n");
for (Phi phi : block.getPhis()) { for (Phi phi : block.getPhis()) {
sb.append(prefix).append(" "); sb.append(prefix).append(" ");
sb.append("@").append(phi.getReceiver().getIndex()).append(":") sb.append("@").append(phi.getReceiver().getIndex()).append(" := ");
.append(phi.getReceiver().getRepresentative()).append(" := ");
List<Incoming> incomings = phi.getIncomings(); List<Incoming> incomings = phi.getIncomings();
for (int j = 0; j < incomings.size(); ++j) { for (int j = 0; j < incomings.size(); ++j) {
if (j > 0) { if (j > 0) {
sb.append(", "); sb.append(", ");
} }
Incoming incoming = incomings.get(j); Incoming incoming = incomings.get(j);
sb.append("@").append(incoming.getValue().getIndex()).append(":") sb.append("@").append(incoming.getValue().getIndex()).append(" from ")
.append(incoming.getValue().getRepresentative()).append(" from ")
.append("$").append(incoming.getSource().getIndex()); .append("$").append(incoming.getSource().getIndex());
} }
sb.append("\n"); sb.append("\n");

View File

@ -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 {
}

View File

@ -77,9 +77,6 @@ public class SSATransformer {
} }
} }
} }
for (int i = 0; i <= arguments.length; ++i) {
program.variableAt(i).setRepresentative(i);
}
arguments = null; arguments = null;
} }
@ -126,7 +123,6 @@ public class SSATransformer {
for (Phi phi : currentBlock.getPhis()) { for (Phi phi : currentBlock.getPhis()) {
Variable var = program.createVariable(); Variable var = program.createVariable();
variableMap[phi.getReceiver().getIndex()] = var; variableMap[phi.getReceiver().getIndex()] = var;
var.setRepresentative(phi.getReceiver().getIndex());
phi.setReceiver(var); phi.setReceiver(var);
} }
for (Instruction insn : currentBlock.getInstructions()) { for (Instruction insn : currentBlock.getInstructions()) {
@ -185,7 +181,6 @@ public class SSATransformer {
private Variable define(Variable var) { private Variable define(Variable var) {
Variable result = program.createVariable(); Variable result = program.createVariable();
result.setRepresentative(var.getIndex());
variableMap[var.getIndex()] = result; variableMap[var.getIndex()] = result;
return result; return result;
} }