diff --git a/core/src/main/java/org/teavm/ast/Expr.java b/core/src/main/java/org/teavm/ast/Expr.java index 5a435b61a..4c6d70af2 100644 --- a/core/src/main/java/org/teavm/ast/Expr.java +++ b/core/src/main/java/org/teavm/ast/Expr.java @@ -63,6 +63,7 @@ public abstract class Expr implements Cloneable { UnaryExpr expr = new UnaryExpr(); expr.setOperand(arg); expr.setOperation(op); + expr.setType(type); return expr; } diff --git a/core/src/main/java/org/teavm/model/util/GraphColorer.java b/core/src/main/java/org/teavm/model/util/GraphColorer.java index ae2ac9c1c..aeca75667 100644 --- a/core/src/main/java/org/teavm/model/util/GraphColorer.java +++ b/core/src/main/java/org/teavm/model/util/GraphColorer.java @@ -28,6 +28,16 @@ class GraphColorer { public void colorize(List graph, int[] colors, int[] categories) { IntegerArray colorCategories = new IntegerArray(graph.size()); + for (int i = 0; i < colors.length; ++i) { + int color = colors[i]; + if (colors[i] < 0) { + continue; + } + while (colorCategories.size() <= color) { + colorCategories.add(-1); + } + colorCategories.set(color, categories[i]); + } BitSet usedColors = new BitSet(); for (int v : getOrdering(graph)) { if (colors[v] >= 0) { diff --git a/core/src/main/java/org/teavm/wasm/Example.java b/core/src/main/java/org/teavm/wasm/Example.java index c5708fc0d..091263b4c 100644 --- a/core/src/main/java/org/teavm/wasm/Example.java +++ b/core/src/main/java/org/teavm/wasm/Example.java @@ -24,11 +24,11 @@ public final class Example { public static void main(String[] args) { int a = 0; int b = 1; - for (int i = 0; i < 10; ++i) { + for (int i = 0; i < 30; ++i) { int c = a + b; a = b; b = c; + WasmRuntime.print(a); } - WasmRuntime.print(a); } } diff --git a/core/src/main/java/org/teavm/wasm/generate/WasmGenerationVisitor.java b/core/src/main/java/org/teavm/wasm/generate/WasmGenerationVisitor.java index 99e58a495..f967c4162 100644 --- a/core/src/main/java/org/teavm/wasm/generate/WasmGenerationVisitor.java +++ b/core/src/main/java/org/teavm/wasm/generate/WasmGenerationVisitor.java @@ -473,7 +473,7 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor { if (statement.getCondition() != null) { statement.getCondition().acceptVisitor(this); - loop.getBody().add(new WasmBranch(result, wrapper)); + loop.getBody().add(new WasmBranch(negate(result), wrapper)); usedBlocks.add(wrapper); } @@ -481,6 +481,7 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor { part.acceptVisitor(this); loop.getBody().add(result); } + loop.getBody().add(new WasmBreak(loop)); currentBreakTarget = oldBreakTarget; currentContinueTarget = oldContinueTarget; diff --git a/core/src/main/java/org/teavm/wasm/generate/WasmGenerator.java b/core/src/main/java/org/teavm/wasm/generate/WasmGenerator.java index 63121156a..5a30bd219 100644 --- a/core/src/main/java/org/teavm/wasm/generate/WasmGenerator.java +++ b/core/src/main/java/org/teavm/wasm/generate/WasmGenerator.java @@ -17,6 +17,7 @@ package org.teavm.wasm.generate; import org.teavm.ast.RegularMethodNode; import org.teavm.ast.decompilation.Decompiler; +import org.teavm.common.IntegerArray; import org.teavm.model.ClassHolder; import org.teavm.model.ClassHolderSource; import org.teavm.model.ElementModifier; @@ -24,6 +25,7 @@ import org.teavm.model.MethodHolder; import org.teavm.model.MethodReference; import org.teavm.model.Program; import org.teavm.model.ValueType; +import org.teavm.model.Variable; import org.teavm.model.util.TypeInferer; import org.teavm.model.util.VariableType; import org.teavm.wasm.model.WasmFunction; @@ -49,11 +51,24 @@ public class WasmGenerator { TypeInferer inferer = new TypeInferer(); inferer.inferTypes(program, methodReference); + IntegerArray variableRepresentatives = new IntegerArray(methodAst.getVariables().size()); + for (int i = 0; i < program.variableCount(); ++i) { + Variable var = program.variableAt(i); + if (var.getRegister() < 0 || inferer.typeOf(i) == null) { + continue; + } + while (variableRepresentatives.size() <= var.getRegister()) { + variableRepresentatives.add(-1); + } + if (variableRepresentatives.get(var.getRegister()) < 0) { + variableRepresentatives.set(var.getRegister(), i); + } + } + WasmFunction function = new WasmFunction(WasmMangling.mangleMethod(methodReference)); int firstVariable = method.hasModifier(ElementModifier.STATIC) ? 1 : 0; for (int i = firstVariable; i < methodAst.getVariables().size(); ++i) { - int varIndex = methodAst.getVariables().get(i); - VariableType type = inferer.typeOf(varIndex); + VariableType type = inferer.typeOf(variableRepresentatives.get(i)); function.add(new WasmLocal(WasmGeneratorUtil.mapType(type))); } diff --git a/core/src/main/java/org/teavm/wasm/render/WasmRenderer.java b/core/src/main/java/org/teavm/wasm/render/WasmRenderer.java index f891a0cf4..ba3c9fea0 100644 --- a/core/src/main/java/org/teavm/wasm/render/WasmRenderer.java +++ b/core/src/main/java/org/teavm/wasm/render/WasmRenderer.java @@ -74,7 +74,7 @@ public class WasmRenderer { importModule = ""; } visitor.open().append("import $" + function.getName() + " \"" + importModule + "\" " - + "\"" + function.getName() + "\""); + + "\"" + function.getImportName() + "\""); renderSignature(function); visitor.close(); } diff --git a/core/src/main/java/org/teavm/wasm/runtime/WasmRuntime.java b/core/src/main/java/org/teavm/wasm/runtime/WasmRuntime.java index 8832f337c..96cf96882 100644 --- a/core/src/main/java/org/teavm/wasm/runtime/WasmRuntime.java +++ b/core/src/main/java/org/teavm/wasm/runtime/WasmRuntime.java @@ -45,6 +45,6 @@ public final class WasmRuntime { return a - (double) (long) (a / b) * b; } - @Import(name = "print.i32") + @Import(name = "print", module = "spectest") public static native void print(int a); }