diff --git a/core/src/main/java/org/teavm/backend/wasm/generate/WasmGenerator.java b/core/src/main/java/org/teavm/backend/wasm/generate/WasmGenerator.java index 33854cb13..0e6e005ef 100644 --- a/core/src/main/java/org/teavm/backend/wasm/generate/WasmGenerator.java +++ b/core/src/main/java/org/teavm/backend/wasm/generate/WasmGenerator.java @@ -22,6 +22,7 @@ import org.teavm.backend.wasm.binary.BinaryWriter; import org.teavm.backend.wasm.model.WasmFunction; import org.teavm.backend.wasm.model.WasmLocal; import org.teavm.backend.wasm.model.WasmType; +import org.teavm.backend.wasm.model.expression.WasmBlock; import org.teavm.interop.Export; import org.teavm.model.AnnotationReader; import org.teavm.model.ClassHolder; @@ -83,6 +84,9 @@ public class WasmGenerator { WasmGenerationVisitor visitor = new WasmGenerationVisitor(context, classGenerator, binaryWriter, function, firstVariable); methodAst.getBody().acceptVisitor(visitor); + if (visitor.result instanceof WasmBlock) { + ((WasmBlock) visitor.result).setType(function.getResult()); + } function.getBody().add(visitor.result); AnnotationReader exportAnnot = method.getAnnotations().get(Export.class.getName()); diff --git a/core/src/main/java/org/teavm/backend/wasm/render/WasmRenderingVisitor.java b/core/src/main/java/org/teavm/backend/wasm/render/WasmRenderingVisitor.java index 6f68d2174..13128b27e 100644 --- a/core/src/main/java/org/teavm/backend/wasm/render/WasmRenderingVisitor.java +++ b/core/src/main/java/org/teavm/backend/wasm/render/WasmRenderingVisitor.java @@ -17,10 +17,8 @@ package org.teavm.backend.wasm.render; import java.util.ArrayList; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Set; import org.teavm.backend.wasm.model.WasmLocal; import org.teavm.backend.wasm.model.WasmType; import org.teavm.backend.wasm.model.expression.WasmBlock; @@ -63,14 +61,13 @@ import org.teavm.backend.wasm.model.expression.WasmSwitch; import org.teavm.backend.wasm.model.expression.WasmUnreachable; class WasmRenderingVisitor implements WasmExpressionVisitor { - private Set usedIdentifiers = new HashSet<>(); StringBuilder sb = new StringBuilder(); private Map blockIdentifiers = new HashMap<>(); private int indentLevel; private boolean lfDeferred; boolean lineNumbersEmitted; List signatureList = new ArrayList<>(); - Map signatureMap = new HashMap<>(); + private Map signatureMap = new HashMap<>(); void preprocess(WasmExpression expression) { expression.acceptVisitor(new WasmDefaultExpressionVisitor() { @@ -103,7 +100,6 @@ class WasmRenderingVisitor implements WasmExpressionVisitor { void clear() { blockIdentifiers.clear(); - usedIdentifiers.clear(); } WasmRenderingVisitor append(String text) { @@ -122,7 +118,7 @@ class WasmRenderingVisitor implements WasmExpressionVisitor { return append(type(type)); } - WasmRenderingVisitor append(WasmExpression expression) { + private WasmRenderingVisitor append(WasmExpression expression) { expression.acceptVisitor(this); return this; } @@ -280,7 +276,7 @@ class WasmRenderingVisitor implements WasmExpressionVisitor { open().append("set_local " + asString(expression.getLocal())).line(expression.getValue()).close(); } - String asString(WasmLocal local) { + private String asString(WasmLocal local) { return String.valueOf(local.getIndex()); } @@ -415,7 +411,9 @@ class WasmRenderingVisitor implements WasmExpressionVisitor { @Override public void visit(WasmDrop expression) { + open().append("drop").lf(); append(expression.getOperand()); + close(); } @Override @@ -580,20 +578,6 @@ class WasmRenderingVisitor implements WasmExpressionVisitor { close(); } - private String getIdentifier(String suggested) { - if (usedIdentifiers.add(suggested)) { - return suggested; - } - int index = 1; - while (true) { - String id = suggested + "#" + index; - if (usedIdentifiers.add(id)) { - return id; - } - ++index; - } - } - private String type(WasmType type) { switch (type) { case INT32: