diff --git a/core/src/main/java/org/teavm/backend/wasm/render/WasmCRenderer.java b/core/src/main/java/org/teavm/backend/wasm/render/WasmCRenderer.java index 0011b59bc..3e431edec 100644 --- a/core/src/main/java/org/teavm/backend/wasm/render/WasmCRenderer.java +++ b/core/src/main/java/org/teavm/backend/wasm/render/WasmCRenderer.java @@ -140,13 +140,13 @@ public class WasmCRenderer { line(functionDeclaration(function) + " {"); indent(); + WasmCRenderingVisitor visitor = new WasmCRenderingVisitor(function.getResult(), function.getModule()); List variables = function.getLocalVariables().subList(function.getParameters().size(), function.getLocalVariables().size()); for (WasmLocal variable : variables) { - line(WasmCRenderingVisitor.mapType(variable.getType()) + " var_" + variable.getIndex() + ";"); + line(WasmCRenderingVisitor.mapType(variable.getType()) + " " + visitor.getVariableName(variable) + ";"); } - WasmCRenderingVisitor visitor = new WasmCRenderingVisitor(function.getResult(), function.getModule()); List body = function.getBody(); List lines = new ArrayList<>(); if (!body.isEmpty()) { diff --git a/core/src/main/java/org/teavm/model/ClassHolderSource.java b/core/src/main/java/org/teavm/model/ClassHolderSource.java index 8e6d055f8..8274a3c1e 100644 --- a/core/src/main/java/org/teavm/model/ClassHolderSource.java +++ b/core/src/main/java/org/teavm/model/ClassHolderSource.java @@ -42,6 +42,6 @@ public interface ClassHolderSource extends ClassReaderSource { } default Stream mutableOverridenMethods(MethodReference method) { - return overridenMethods(method).map(m -> (MethodHolder) m); + return overriddenMethods(method).map(m -> (MethodHolder) m); } } \ No newline at end of file diff --git a/core/src/main/java/org/teavm/model/ClassReaderSource.java b/core/src/main/java/org/teavm/model/ClassReaderSource.java index daec412ff..da764fa63 100644 --- a/core/src/main/java/org/teavm/model/ClassReaderSource.java +++ b/core/src/main/java/org/teavm/model/ClassReaderSource.java @@ -24,10 +24,6 @@ import java.util.Set; import java.util.stream.Stream; import java.util.stream.StreamSupport; -/** - * - * @author Alexey Andreev - */ public interface ClassReaderSource { ClassReader get(String name); @@ -104,7 +100,7 @@ public interface ClassReaderSource { .findFirst().orElse(null); } - default Stream overridenMethods(MethodReference method) { + default Stream overriddenMethods(MethodReference method) { return getAncestorClasses(method.getClassName()) .map(cls -> cls.getMethod(method.getDescriptor())) .filter(candidate -> candidate != null); diff --git a/core/src/main/java/org/teavm/model/optimization/GlobalValueNumbering.java b/core/src/main/java/org/teavm/model/optimization/GlobalValueNumbering.java index aaf78758b..60264c116 100644 --- a/core/src/main/java/org/teavm/model/optimization/GlobalValueNumbering.java +++ b/core/src/main/java/org/teavm/model/optimization/GlobalValueNumbering.java @@ -150,12 +150,20 @@ public class GlobalValueNumbering implements MethodOptimization { } } + String[] debugNames = new String[program.variableCount()]; + for (int i = 0; i < program.variableCount(); ++i) { + debugNames[i] = program.variableAt(i).getDebugName(); + program.variableAt(i).setDebugName(null); + } for (int i = 0; i < map.length; ++i) { if (map[i] != i) { - Variable var = program.variableAt(i); Variable mapVar = program.variableAt(map[i]); - mapVar.setDebugName(var.getDebugName()); + if (debugNames[i] != null && mapVar.getDebugName() == null) { + mapVar.setDebugName(debugNames[i]); + } program.deleteVariable(i); + } else { + program.variableAt(i).setDebugName(debugNames[i]); } } @@ -165,7 +173,7 @@ public class GlobalValueNumbering implements MethodOptimization { } private void bind(int var, String value) { - String name = program.variableAt(var).getDebugName(); + String name = program.variableAt(map[var]).getDebugName(); if (name == null) { name = ""; } diff --git a/core/src/main/java/org/teavm/model/util/PhiUpdater.java b/core/src/main/java/org/teavm/model/util/PhiUpdater.java index 10b0c9071..40d20a840 100644 --- a/core/src/main/java/org/teavm/model/util/PhiUpdater.java +++ b/core/src/main/java/org/teavm/model/util/PhiUpdater.java @@ -28,6 +28,7 @@ import java.util.stream.Collectors; import org.teavm.common.DominatorTree; import org.teavm.common.Graph; import org.teavm.common.GraphUtils; +import org.teavm.common.IntegerArray; import org.teavm.model.BasicBlock; import org.teavm.model.Incoming; import org.teavm.model.Instruction; @@ -88,6 +89,11 @@ public class PhiUpdater { private List>> synthesizedJoints = new ArrayList<>(); private Variable[] originalExceptionVariables; private boolean[] usedDefinitions; + private IntegerArray variableToSourceMap = new IntegerArray(10); + + public int getSourceVariable(int var) { + return variableToSourceMap.get(var); + } public void updatePhis(Program program, Variable[] arguments) { if (program.basicBlockCount() == 0) { @@ -103,6 +109,9 @@ public class PhiUpdater { variableMap[i] = arguments[i]; usedDefinitions[i] = true; } + for (int i = 0; i < program.variableCount(); ++i) { + variableToSourceMap.add(-1); + } phiMap = new Phi[program.basicBlockCount()][]; phiIndexMap = new int[program.basicBlockCount()][]; jointMap.clear(); @@ -201,7 +210,7 @@ public class PhiUpdater { for (Phi phi : synthesizedPhis.get(index)) { Variable var = program.createVariable(); var.setDebugName(phi.getReceiver().getDebugName()); - variableMap[phi.getReceiver().getIndex()] = var; + mapVariable(phi.getReceiver().getIndex(), var); phi.setReceiver(var); } for (Phi phi : currentBlock.getPhis()) { @@ -334,7 +343,7 @@ public class PhiUpdater { Variable original = var; var = introduce(var); propagateToTryCatch(original, var, old); - variableMap[original.getIndex()] = var; + mapVariable(original.getIndex(), var); return var; } @@ -368,6 +377,14 @@ public class PhiUpdater { } } + private void mapVariable(int index, Variable var) { + variableMap[index] = var; + while (variableToSourceMap.size() <= var.getIndex()) { + variableToSourceMap.add(-1); + } + variableToSourceMap.set(var.getIndex(), index); + } + private Variable introduce(Variable var) { if (!usedDefinitions[var.getIndex()]) { usedDefinitions[var.getIndex()] = true; diff --git a/core/src/main/java/org/teavm/parsing/Parser.java b/core/src/main/java/org/teavm/parsing/Parser.java index ad99c1a1b..016d4bd54 100644 --- a/core/src/main/java/org/teavm/parsing/Parser.java +++ b/core/src/main/java/org/teavm/parsing/Parser.java @@ -86,6 +86,8 @@ public class Parser { applyDebugNames(program, phiUpdater, programParser, argumentMapping); parseAnnotations(method.getAnnotations(), node.visibleAnnotations, node.invisibleAnnotations); + applyDebugNames(program, phiUpdater, programParser, + applySignature(program, method.getDescriptor().getParameterTypes())); while (program.variableCount() <= method.parameterCount()) { program.createVariable(); } @@ -100,7 +102,7 @@ public class Parser { return method; } - private void applyDebugNames(Program program, PhiUpdater phiUpdater, ProgramParser parser, + private static void applyDebugNames(Program program, PhiUpdater phiUpdater, ProgramParser parser, Variable[] argumentMapping) { if (program.basicBlockCount() == 0) { return; @@ -130,14 +132,14 @@ public class Parser { for (Map.Entry debugName : debugNames.entrySet()) { int receiver = varMap.getOrDefault(debugName.getKey(), -1); if (receiver >= 0) { - program.variableAt(receiver).getDebugNames().add(debugName.getValue()); + program.variableAt(receiver).setDebugName(debugName.getValue()); } } } } } - private IntIntMap[] getBlockEntryVariableMappings(Program program, PhiUpdater phiUpdater, + private static IntIntMap[] getBlockEntryVariableMappings(Program program, PhiUpdater phiUpdater, Variable[] argumentMapping) { class Step { int node; @@ -171,36 +173,29 @@ public class Parser { IntIntMap varMap = new IntIntOpenHashMap(step.varMap); BasicBlock block = program.basicBlockAt(node); - /* - for (TryCatchJoint joint : block.getTryCatchJoints()) { - int receiver = joint.getReceiver().getIndex(); - int sourceVar = phiUpdater.getSourceVariable(receiver); - if (sourceVar >= 0) { - varMap.put(sourceVar, receiver); - } - } for (Phi phi : block.getPhis()) { int receiver = phi.getReceiver().getIndex(); + /* int sourceVar = phiUpdater.getSourceVariable(receiver); if (sourceVar >= 0) { varMap.put(sourceVar, receiver); } + */ } - */ result[node] = new IntIntOpenHashMap(varMap); - /* for (Instruction insn : block.getInstructions()) { insn.acceptVisitor(defExtractor); for (Variable definedVar : defExtractor.getDefinedVariables()) { + /* int sourceVar = phiUpdater.getSourceVariable(definedVar.getIndex()); if (sourceVar >= 0) { varMap.put(sourceVar, definedVar.getIndex()); } + */ } } - */ for (int successor : dom.outgoingEdges(node)) { stack[top++] = new Step(successor, new IntIntOpenHashMap(varMap));