WASM: fix bugs

This commit is contained in:
Alexey Andreev 2017-01-29 22:17:04 +03:00
parent 2610fe5428
commit ecd58e3573
2 changed files with 9 additions and 21 deletions

View File

@ -22,6 +22,7 @@ import org.teavm.backend.wasm.binary.BinaryWriter;
import org.teavm.backend.wasm.model.WasmFunction; import org.teavm.backend.wasm.model.WasmFunction;
import org.teavm.backend.wasm.model.WasmLocal; import org.teavm.backend.wasm.model.WasmLocal;
import org.teavm.backend.wasm.model.WasmType; import org.teavm.backend.wasm.model.WasmType;
import org.teavm.backend.wasm.model.expression.WasmBlock;
import org.teavm.interop.Export; import org.teavm.interop.Export;
import org.teavm.model.AnnotationReader; import org.teavm.model.AnnotationReader;
import org.teavm.model.ClassHolder; import org.teavm.model.ClassHolder;
@ -83,6 +84,9 @@ public class WasmGenerator {
WasmGenerationVisitor visitor = new WasmGenerationVisitor(context, classGenerator, binaryWriter, function, WasmGenerationVisitor visitor = new WasmGenerationVisitor(context, classGenerator, binaryWriter, function,
firstVariable); firstVariable);
methodAst.getBody().acceptVisitor(visitor); methodAst.getBody().acceptVisitor(visitor);
if (visitor.result instanceof WasmBlock) {
((WasmBlock) visitor.result).setType(function.getResult());
}
function.getBody().add(visitor.result); function.getBody().add(visitor.result);
AnnotationReader exportAnnot = method.getAnnotations().get(Export.class.getName()); AnnotationReader exportAnnot = method.getAnnotations().get(Export.class.getName());

View File

@ -17,10 +17,8 @@ package org.teavm.backend.wasm.render;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.teavm.backend.wasm.model.WasmLocal; import org.teavm.backend.wasm.model.WasmLocal;
import org.teavm.backend.wasm.model.WasmType; import org.teavm.backend.wasm.model.WasmType;
import org.teavm.backend.wasm.model.expression.WasmBlock; 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; import org.teavm.backend.wasm.model.expression.WasmUnreachable;
class WasmRenderingVisitor implements WasmExpressionVisitor { class WasmRenderingVisitor implements WasmExpressionVisitor {
private Set<String> usedIdentifiers = new HashSet<>();
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
private Map<WasmBlock, String> blockIdentifiers = new HashMap<>(); private Map<WasmBlock, String> blockIdentifiers = new HashMap<>();
private int indentLevel; private int indentLevel;
private boolean lfDeferred; private boolean lfDeferred;
boolean lineNumbersEmitted; boolean lineNumbersEmitted;
List<WasmSignature> signatureList = new ArrayList<>(); List<WasmSignature> signatureList = new ArrayList<>();
Map<WasmSignature, Integer> signatureMap = new HashMap<>(); private Map<WasmSignature, Integer> signatureMap = new HashMap<>();
void preprocess(WasmExpression expression) { void preprocess(WasmExpression expression) {
expression.acceptVisitor(new WasmDefaultExpressionVisitor() { expression.acceptVisitor(new WasmDefaultExpressionVisitor() {
@ -103,7 +100,6 @@ class WasmRenderingVisitor implements WasmExpressionVisitor {
void clear() { void clear() {
blockIdentifiers.clear(); blockIdentifiers.clear();
usedIdentifiers.clear();
} }
WasmRenderingVisitor append(String text) { WasmRenderingVisitor append(String text) {
@ -122,7 +118,7 @@ class WasmRenderingVisitor implements WasmExpressionVisitor {
return append(type(type)); return append(type(type));
} }
WasmRenderingVisitor append(WasmExpression expression) { private WasmRenderingVisitor append(WasmExpression expression) {
expression.acceptVisitor(this); expression.acceptVisitor(this);
return this; return this;
} }
@ -280,7 +276,7 @@ class WasmRenderingVisitor implements WasmExpressionVisitor {
open().append("set_local " + asString(expression.getLocal())).line(expression.getValue()).close(); 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()); return String.valueOf(local.getIndex());
} }
@ -415,7 +411,9 @@ class WasmRenderingVisitor implements WasmExpressionVisitor {
@Override @Override
public void visit(WasmDrop expression) { public void visit(WasmDrop expression) {
open().append("drop").lf();
append(expression.getOperand()); append(expression.getOperand());
close();
} }
@Override @Override
@ -580,20 +578,6 @@ class WasmRenderingVisitor implements WasmExpressionVisitor {
close(); 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) { private String type(WasmType type) {
switch (type) { switch (type) {
case INT32: case INT32: