WASM: implementing support of 0xC version

This commit is contained in:
Alexey Andreev 2016-10-12 23:45:13 +03:00
parent b4916ee4e7
commit 56b1f54dda
5 changed files with 45 additions and 11 deletions

View File

@ -528,14 +528,12 @@ public class WasmTarget implements TeaVMTarget {
WasmExpression lowerCondition = new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.LT_SIGNED, WasmExpression lowerCondition = new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.LT_SIGNED,
new WasmGetLocal(subtypeVar), new WasmInt32Constant(lower)); new WasmGetLocal(subtypeVar), new WasmInt32Constant(lower));
WasmConditional testLower = new WasmConditional(lowerCondition); WasmConditional testLower = new WasmConditional(lowerCondition);
testLower.setType(WasmType.INT32);
testLower.getThenBlock().getBody().add(new WasmReturn(new WasmInt32Constant(0))); testLower.getThenBlock().getBody().add(new WasmReturn(new WasmInt32Constant(0)));
body.add(testLower); body.add(testLower);
WasmExpression upperCondition = new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.GT_SIGNED, WasmExpression upperCondition = new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.GT_SIGNED,
new WasmGetLocal(subtypeVar), new WasmInt32Constant(upper)); new WasmGetLocal(subtypeVar), new WasmInt32Constant(upper));
WasmConditional testUpper = new WasmConditional(upperCondition); WasmConditional testUpper = new WasmConditional(upperCondition);
testLower.setType(WasmType.INT32);
testUpper.getThenBlock().getBody().add(new WasmReturn(new WasmInt32Constant(0))); testUpper.getThenBlock().getBody().add(new WasmReturn(new WasmInt32Constant(0)));
body.add(testUpper); body.add(testUpper);

View File

@ -340,7 +340,7 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor {
branch.setResult(new WasmInt32Constant(0)); branch.setResult(new WasmInt32Constant(0));
branch.setLocation(expr.getLocation()); branch.setLocation(expr.getLocation());
branch.getResult().setLocation(expr.getLocation()); branch.getResult().setLocation(expr.getLocation());
block.getBody().add(branch); block.getBody().add(new WasmDrop(branch));
accept(expr.getSecondOperand()); accept(expr.getSecondOperand());
block.getBody().add(result); block.getBody().add(result);
@ -359,7 +359,7 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor {
branch.setResult(new WasmInt32Constant(1)); branch.setResult(new WasmInt32Constant(1));
branch.setLocation(expr.getLocation()); branch.setLocation(expr.getLocation());
branch.getResult().setLocation(expr.getLocation()); branch.getResult().setLocation(expr.getLocation());
block.getBody().add(branch); block.getBody().add(new WasmDrop(branch));
accept(expr.getSecondOperand()); accept(expr.getSecondOperand());
block.getBody().add(result); block.getBody().add(result);
@ -1213,13 +1213,13 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor {
new WasmGetLocal(tagVar), new WasmInt32Constant(ranges.get(0).lower)); new WasmGetLocal(tagVar), new WasmInt32Constant(ranges.get(0).lower));
WasmBranch lowerThanMin = new WasmBranch(lowerThanMinCond, block); WasmBranch lowerThanMin = new WasmBranch(lowerThanMinCond, block);
lowerThanMin.setResult(new WasmInt32Constant(0)); lowerThanMin.setResult(new WasmInt32Constant(0));
block.getBody().add(lowerThanMin); block.getBody().add(new WasmDrop(lowerThanMin));
WasmExpression upperThanMaxCond = new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.GT_SIGNED, WasmExpression upperThanMaxCond = new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.GT_SIGNED,
new WasmGetLocal(tagVar), new WasmInt32Constant(ranges.get(ranges.size() - 1).upper)); new WasmGetLocal(tagVar), new WasmInt32Constant(ranges.get(ranges.size() - 1).upper));
WasmBranch upperThanMax = new WasmBranch(upperThanMaxCond, block); WasmBranch upperThanMax = new WasmBranch(upperThanMaxCond, block);
upperThanMax.setResult(new WasmInt32Constant(0)); upperThanMax.setResult(new WasmInt32Constant(0));
block.getBody().add(upperThanMax); block.getBody().add(new WasmDrop(upperThanMax));
for (int i = 1; i < ranges.size(); ++i) { for (int i = 1; i < ranges.size(); ++i) {
WasmExpression upperThanExcluded = new WasmIntBinary(WasmIntType.INT32, WasmExpression upperThanExcluded = new WasmIntBinary(WasmIntType.INT32,
@ -1233,7 +1233,7 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor {
WasmBranch branch = new WasmBranch(lowerThanExcluded, block); WasmBranch branch = new WasmBranch(lowerThanExcluded, block);
branch.setResult(new WasmInt32Constant(0)); branch.setResult(new WasmInt32Constant(0));
conditional.getThenBlock().getBody().add(branch); conditional.getThenBlock().getBody().add(new WasmDrop(branch));
block.getBody().add(conditional); block.getBody().add(conditional);
} }

View File

@ -22,6 +22,11 @@ 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.WasmExpression;
import org.teavm.backend.wasm.model.expression.WasmFloat32Constant;
import org.teavm.backend.wasm.model.expression.WasmFloat64Constant;
import org.teavm.backend.wasm.model.expression.WasmInt32Constant;
import org.teavm.backend.wasm.model.expression.WasmInt64Constant;
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;
@ -85,6 +90,27 @@ public class WasmGenerator {
methodAst.getBody().acceptVisitor(visitor); methodAst.getBody().acceptVisitor(visitor);
function.getBody().add(visitor.result); function.getBody().add(visitor.result);
if (function.getResult() != null) {
WasmExpression finalExpr;
switch (function.getResult()) {
case INT32:
finalExpr = new WasmInt32Constant(0);
break;
case INT64:
finalExpr = new WasmInt64Constant(0);
break;
case FLOAT32:
finalExpr = new WasmFloat32Constant(0);
break;
case FLOAT64:
finalExpr = new WasmFloat64Constant(0);
break;
default:
throw new AssertionError();
}
function.getBody().add(finalExpr);
}
AnnotationReader exportAnnot = method.getAnnotations().get(Export.class.getName()); AnnotationReader exportAnnot = method.getAnnotations().get(Export.class.getName());
if (exportAnnot != null) { if (exportAnnot != null) {
function.setExportName(exportAnnot.getValue("name").getString()); function.setExportName(exportAnnot.getValue("name").getString());

View File

@ -114,12 +114,12 @@ public class WasmBinaryRenderer {
if (function.getImportName() == null) { if (function.getImportName() == null) {
continue; continue;
} }
functions.add(function);
if (version == WasmBinaryVersion.V_0xB) { if (version == WasmBinaryVersion.V_0xB) {
importIndexes.put(function.getName(), index++); importIndexes.put(function.getName(), index++);
} else { } else {
functionIndexes.put(function.getName(), functions.size()); functionIndexes.put(function.getName(), functions.size());
} }
functions.add(function);
} }
if (functions.isEmpty()) { if (functions.isEmpty()) {
return; return;
@ -186,7 +186,8 @@ public class WasmBinaryRenderer {
} else { } else {
section.writeByte(1); section.writeByte(1);
section.writeByte(0x20); section.writeByte(0x20);
section.writeByte(0); section.writeByte(3);
section.writeLEB(functionIndexes.size());
section.writeLEB(functionIndexes.size()); section.writeLEB(functionIndexes.size());
} }
writeSection(SECTION_TABLE, "table", section.getData()); writeSection(SECTION_TABLE, "table", section.getData());
@ -313,6 +314,10 @@ public class WasmBinaryRenderer {
} }
} }
Map<String, Integer> importIndexes = this.importIndexes;
if (version == WasmBinaryVersion.V_0xC) {
importIndexes = this.functionIndexes;
}
WasmBinaryRenderingVisitor visitor = new WasmBinaryRenderingVisitor(code, version, functionIndexes, WasmBinaryRenderingVisitor visitor = new WasmBinaryRenderingVisitor(code, version, functionIndexes,
importIndexes, signatureIndexes); importIndexes, signatureIndexes);
for (WasmExpression part : function.getBody()) { for (WasmExpression part : function.getBody()) {

View File

@ -645,7 +645,7 @@ class WasmBinaryRenderingVisitor implements WasmExpressionVisitor {
for (WasmExpression argument : expression.getArguments()) { for (WasmExpression argument : expression.getArguments()) {
argument.acceptVisitor(this); argument.acceptVisitor(this);
} }
Integer functionIndex = !expression.isImported() || version == WasmBinaryVersion.V_0xC Integer functionIndex = !expression.isImported()
? functionIndexes.get(expression.getFunctionName()) ? functionIndexes.get(expression.getFunctionName())
: importedIndexes.get(expression.getFunctionName()); : importedIndexes.get(expression.getFunctionName());
if (functionIndex == null) { if (functionIndex == null) {
@ -664,10 +664,15 @@ class WasmBinaryRenderingVisitor implements WasmExpressionVisitor {
@Override @Override
public void visit(WasmIndirectCall expression) { public void visit(WasmIndirectCall expression) {
expression.getSelector().acceptVisitor(this); if (version == WasmBinaryVersion.V_0xB) {
expression.getSelector().acceptVisitor(this);
}
for (WasmExpression argument : expression.getArguments()) { for (WasmExpression argument : expression.getArguments()) {
argument.acceptVisitor(this); argument.acceptVisitor(this);
} }
if (version == WasmBinaryVersion.V_0xC) {
expression.getSelector().acceptVisitor(this);
}
writer.writeByte(0x17); writer.writeByte(0x17);
if (version == WasmBinaryVersion.V_0xB) { if (version == WasmBinaryVersion.V_0xB) {
writer.writeLEB(expression.getArguments().size()); writer.writeLEB(expression.getArguments().size());