Wasm backend: add intrinsics for some Float and Double methods

This commit is contained in:
Alexey Andreev 2018-05-16 19:55:40 +03:00
parent d7b57ae938
commit f0d97a72ab
9 changed files with 295 additions and 19 deletions

View File

@ -93,13 +93,9 @@ public class TFloat extends TNumber implements TComparable<TFloat> {
@Import(module = "teavm", name = "isnan") @Import(module = "teavm", name = "isnan")
public static native boolean isNaN(float v); public static native boolean isNaN(float v);
public static boolean isInfinite(float v) { @JSBody(params = "v", script = "return !isFinite(v);")
return !isFinite(v); @Import(module = "teavm", name = "isinf")
} public static native boolean isInfinite(float v);
@JSBody(params = "v", script = "return isFinite(v);")
@Import(module = "teavm", name = "isfinite")
private static native boolean isFinite(float v);
@JSBody(script = "return NaN;") @JSBody(script = "return NaN;")
@Import(module = "teavm", name = "TeaVM_getNaN") @Import(module = "teavm", name = "TeaVM_getNaN")

View File

@ -44,7 +44,9 @@ import org.teavm.backend.wasm.generators.WasmMethodGeneratorContext;
import org.teavm.backend.wasm.intrinsics.AddressIntrinsic; import org.teavm.backend.wasm.intrinsics.AddressIntrinsic;
import org.teavm.backend.wasm.intrinsics.AllocatorIntrinsic; import org.teavm.backend.wasm.intrinsics.AllocatorIntrinsic;
import org.teavm.backend.wasm.intrinsics.ClassIntrinsic; import org.teavm.backend.wasm.intrinsics.ClassIntrinsic;
import org.teavm.backend.wasm.intrinsics.DoubleIntrinsic;
import org.teavm.backend.wasm.intrinsics.ExceptionHandlingIntrinsic; import org.teavm.backend.wasm.intrinsics.ExceptionHandlingIntrinsic;
import org.teavm.backend.wasm.intrinsics.FloatIntrinsic;
import org.teavm.backend.wasm.intrinsics.FunctionIntrinsic; import org.teavm.backend.wasm.intrinsics.FunctionIntrinsic;
import org.teavm.backend.wasm.intrinsics.GCIntrinsic; import org.teavm.backend.wasm.intrinsics.GCIntrinsic;
import org.teavm.backend.wasm.intrinsics.MutatorIntrinsic; import org.teavm.backend.wasm.intrinsics.MutatorIntrinsic;
@ -334,6 +336,8 @@ public class WasmTarget implements TeaVMTarget, TeaVMWasmHost {
context.addIntrinsic(new PlatformClassMetadataIntrinsic()); context.addIntrinsic(new PlatformClassMetadataIntrinsic());
context.addIntrinsic(new ClassIntrinsic()); context.addIntrinsic(new ClassIntrinsic());
context.addIntrinsic(new RuntimeClassIntrinsic()); context.addIntrinsic(new RuntimeClassIntrinsic());
context.addIntrinsic(new FloatIntrinsic());
context.addIntrinsic(new DoubleIntrinsic());
context.addGenerator(new ArrayGenerator()); context.addGenerator(new ArrayGenerator());
IntrinsicFactoryContext intrinsicFactoryContext = new IntrinsicFactoryContext(); IntrinsicFactoryContext intrinsicFactoryContext = new IntrinsicFactoryContext();

View File

@ -1517,6 +1517,16 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor {
public NameProvider getNames() { public NameProvider getNames() {
return context.names; return context.names;
} }
@Override
public WasmLocal getTemporary(WasmType type) {
return WasmGenerationVisitor.this.getTemporary(type);
}
@Override
public void releaseTemporary(WasmLocal local) {
WasmGenerationVisitor.this.releaseTemporary(local);
}
}; };
private WasmLocal getTemporary(WasmType type) { private WasmLocal getTemporary(WasmType type) {

View File

@ -0,0 +1,112 @@
/*
* Copyright 2018 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.backend.wasm.intrinsics;
import org.teavm.ast.InvocationExpr;
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.backend.wasm.model.expression.WasmBranch;
import org.teavm.backend.wasm.model.expression.WasmConversion;
import org.teavm.backend.wasm.model.expression.WasmDrop;
import org.teavm.backend.wasm.model.expression.WasmExpression;
import org.teavm.backend.wasm.model.expression.WasmFloat64Constant;
import org.teavm.backend.wasm.model.expression.WasmGetLocal;
import org.teavm.backend.wasm.model.expression.WasmInt32Constant;
import org.teavm.backend.wasm.model.expression.WasmInt64Constant;
import org.teavm.backend.wasm.model.expression.WasmIntBinary;
import org.teavm.backend.wasm.model.expression.WasmIntBinaryOperation;
import org.teavm.backend.wasm.model.expression.WasmIntType;
import org.teavm.backend.wasm.model.expression.WasmSetLocal;
import org.teavm.model.MethodReference;
public class DoubleIntrinsic implements WasmIntrinsic {
private static final long EXPONENT_BITS = 0x7FF0000000000000L;
private static final long FRACTION_BITS = 0x000FFFFFFFFFFFFFL;
@Override
public boolean isApplicable(MethodReference methodReference) {
if (!methodReference.getClassName().equals(Double.class.getName())) {
return false;
}
switch (methodReference.getName()) {
case "getNaN":
case "isNaN":
case "isInfinite":
case "doubleToLongBits":
case "longBitsToDouble":
return true;
default:
return false;
}
}
@Override
public WasmExpression apply(InvocationExpr invocation, WasmIntrinsicManager manager) {
switch (invocation.getMethod().getName()) {
case "getNaN":
return new WasmFloat64Constant(Double.NaN);
case "isNaN":
return testSpecialIEEE(manager.generate(invocation.getArguments().get(0)), manager,
WasmIntBinaryOperation.NE);
case "isInfinite":
return testSpecialIEEE(manager.generate(invocation.getArguments().get(0)), manager,
WasmIntBinaryOperation.EQ);
case "doubleToLongBits": {
WasmConversion conversion = new WasmConversion(WasmType.FLOAT64, WasmType.INT64, false,
manager.generate(invocation.getArguments().get(0)));
conversion.setReinterpret(true);
return conversion;
}
case "longBitsToDouble": {
WasmConversion conversion = new WasmConversion(WasmType.INT64, WasmType.FLOAT64, false,
manager.generate(invocation.getArguments().get(0)));
conversion.setReinterpret(true);
return conversion;
}
default:
throw new AssertionError();
}
}
private WasmExpression testSpecialIEEE(WasmExpression expression, WasmIntrinsicManager manager,
WasmIntBinaryOperation fractionOp) {
WasmLocal bitsVar = manager.getTemporary(WasmType.INT64);
WasmBlock block = new WasmBlock(false);
block.setType(WasmType.INT32);
WasmConversion conversion = new WasmConversion(WasmType.FLOAT64, WasmType.INT64, false, expression);
conversion.setReinterpret(true);
block.getBody().add(new WasmSetLocal(bitsVar, conversion));
WasmExpression exponentBits = new WasmIntBinary(WasmIntType.INT64, WasmIntBinaryOperation.AND,
new WasmGetLocal(bitsVar), new WasmInt64Constant(EXPONENT_BITS));
WasmExpression fractionBits = new WasmIntBinary(WasmIntType.INT64, WasmIntBinaryOperation.AND,
new WasmGetLocal(bitsVar), new WasmInt64Constant(FRACTION_BITS));
WasmExpression testExponent = new WasmIntBinary(WasmIntType.INT64, WasmIntBinaryOperation.NE,
exponentBits, new WasmInt64Constant(EXPONENT_BITS));
WasmExpression testFraction = new WasmIntBinary(WasmIntType.INT64, fractionOp,
fractionBits, new WasmInt64Constant(0));
WasmBranch breakIfWrongExponent = new WasmBranch(testExponent, block);
breakIfWrongExponent.setResult(new WasmInt32Constant(0));
block.getBody().add(new WasmDrop(breakIfWrongExponent));
block.getBody().add(testFraction);
return block;
}
}

View File

@ -0,0 +1,111 @@
/*
* Copyright 2018 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.backend.wasm.intrinsics;
import org.teavm.ast.InvocationExpr;
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.backend.wasm.model.expression.WasmBranch;
import org.teavm.backend.wasm.model.expression.WasmConversion;
import org.teavm.backend.wasm.model.expression.WasmDrop;
import org.teavm.backend.wasm.model.expression.WasmExpression;
import org.teavm.backend.wasm.model.expression.WasmFloat32Constant;
import org.teavm.backend.wasm.model.expression.WasmGetLocal;
import org.teavm.backend.wasm.model.expression.WasmInt32Constant;
import org.teavm.backend.wasm.model.expression.WasmIntBinary;
import org.teavm.backend.wasm.model.expression.WasmIntBinaryOperation;
import org.teavm.backend.wasm.model.expression.WasmIntType;
import org.teavm.backend.wasm.model.expression.WasmSetLocal;
import org.teavm.model.MethodReference;
public class FloatIntrinsic implements WasmIntrinsic {
private static final int EXPONENT_BITS = 0x7F800000;
private static final int FRACTION_BITS = 0x007FFFFF;
@Override
public boolean isApplicable(MethodReference methodReference) {
if (!methodReference.getClassName().equals(Float.class.getName())) {
return false;
}
switch (methodReference.getName()) {
case "getNaN":
case "isNaN":
case "isInfinite":
case "floatToIntBits":
case "intBitsToFloat":
return true;
default:
return false;
}
}
@Override
public WasmExpression apply(InvocationExpr invocation, WasmIntrinsicManager manager) {
switch (invocation.getMethod().getName()) {
case "getNaN":
return new WasmFloat32Constant(Float.NaN);
case "isNaN":
return testSpecialIEEE(manager.generate(invocation.getArguments().get(0)), manager,
WasmIntBinaryOperation.NE);
case "isInfinite":
return testSpecialIEEE(manager.generate(invocation.getArguments().get(0)), manager,
WasmIntBinaryOperation.EQ);
case "floatToIntBits": {
WasmConversion conversion = new WasmConversion(WasmType.FLOAT32, WasmType.INT32, false,
manager.generate(invocation.getArguments().get(0)));
conversion.setReinterpret(true);
return conversion;
}
case "intBitsToFloat": {
WasmConversion conversion = new WasmConversion(WasmType.INT32, WasmType.FLOAT32, false,
manager.generate(invocation.getArguments().get(0)));
conversion.setReinterpret(true);
return conversion;
}
default:
throw new AssertionError();
}
}
private WasmExpression testSpecialIEEE(WasmExpression expression, WasmIntrinsicManager manager,
WasmIntBinaryOperation fractionOp) {
WasmLocal bitsVar = manager.getTemporary(WasmType.INT32);
WasmBlock block = new WasmBlock(false);
block.setType(WasmType.INT32);
WasmConversion conversion = new WasmConversion(WasmType.FLOAT32, WasmType.INT32, false, expression);
conversion.setReinterpret(true);
block.getBody().add(new WasmSetLocal(bitsVar, conversion));
WasmExpression exponentBits = new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.AND,
new WasmGetLocal(bitsVar), new WasmInt32Constant(EXPONENT_BITS));
WasmExpression fractionBits = new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.AND,
new WasmGetLocal(bitsVar), new WasmInt32Constant(FRACTION_BITS));
WasmExpression testExponent = new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.NE,
exponentBits, new WasmInt32Constant(EXPONENT_BITS));
WasmExpression testFraction = new WasmIntBinary(WasmIntType.INT32, fractionOp,
fractionBits, new WasmInt32Constant(0));
WasmBranch breakIfWrongExponent = new WasmBranch(testExponent, block);
breakIfWrongExponent.setResult(new WasmInt32Constant(0));
block.getBody().add(new WasmDrop(breakIfWrongExponent));
block.getBody().add(testFraction);
return block;
}
}

View File

@ -19,6 +19,8 @@ import org.teavm.ast.Expr;
import org.teavm.backend.wasm.binary.BinaryWriter; import org.teavm.backend.wasm.binary.BinaryWriter;
import org.teavm.backend.wasm.generate.NameProvider; import org.teavm.backend.wasm.generate.NameProvider;
import org.teavm.backend.wasm.generate.WasmStringPool; import org.teavm.backend.wasm.generate.WasmStringPool;
import org.teavm.backend.wasm.model.WasmLocal;
import org.teavm.backend.wasm.model.WasmType;
import org.teavm.backend.wasm.model.expression.WasmExpression; import org.teavm.backend.wasm.model.expression.WasmExpression;
import org.teavm.diagnostics.Diagnostics; import org.teavm.diagnostics.Diagnostics;
@ -32,4 +34,8 @@ public interface WasmIntrinsicManager {
Diagnostics getDiagnostics(); Diagnostics getDiagnostics();
NameProvider getNames(); NameProvider getNames();
WasmLocal getTemporary(WasmType type);
void releaseTemporary(WasmLocal local);
} }

View File

@ -23,6 +23,7 @@ public class WasmConversion extends WasmExpression {
private WasmType targetType; private WasmType targetType;
private boolean signed; private boolean signed;
private WasmExpression operand; private WasmExpression operand;
private boolean reinterpret;
public WasmConversion(WasmType sourceType, WasmType targetType, boolean signed, WasmExpression operand) { public WasmConversion(WasmType sourceType, WasmType targetType, boolean signed, WasmExpression operand) {
Objects.requireNonNull(sourceType); Objects.requireNonNull(sourceType);
@ -60,6 +61,14 @@ public class WasmConversion extends WasmExpression {
this.signed = signed; this.signed = signed;
} }
public boolean isReinterpret() {
return reinterpret;
}
public void setReinterpret(boolean reinterpret) {
this.reinterpret = reinterpret;
}
public WasmExpression getOperand() { public WasmExpression getOperand() {
return operand; return operand;
} }

View File

@ -569,10 +569,6 @@ class WasmBinaryRenderingVisitor implements WasmExpressionVisitor {
@Override @Override
public void visit(WasmConversion expression) { public void visit(WasmConversion expression) {
expression.getOperand().acceptVisitor(this); expression.getOperand().acceptVisitor(this);
render0xD(expression);
}
private void render0xD(WasmConversion expression) {
switch (expression.getSourceType()) { switch (expression.getSourceType()) {
case INT32: case INT32:
switch (expression.getTargetType()) { switch (expression.getTargetType()) {
@ -582,7 +578,11 @@ class WasmBinaryRenderingVisitor implements WasmExpressionVisitor {
writer.writeByte(expression.isSigned() ? 0xAC : 0xAD); writer.writeByte(expression.isSigned() ? 0xAC : 0xAD);
break; break;
case FLOAT32: case FLOAT32:
if (expression.isReinterpret()) {
writer.writeByte(0xBE);
} else {
writer.writeByte(expression.isSigned() ? 0xB2 : 0xB3); writer.writeByte(expression.isSigned() ? 0xB2 : 0xB3);
}
break; break;
case FLOAT64: case FLOAT64:
writer.writeByte(expression.isSigned() ? 0xB7 : 0xB8); writer.writeByte(expression.isSigned() ? 0xB7 : 0xB8);
@ -600,14 +600,22 @@ class WasmBinaryRenderingVisitor implements WasmExpressionVisitor {
writer.writeByte(expression.isSigned() ? 0xB4 : 0xB5); writer.writeByte(expression.isSigned() ? 0xB4 : 0xB5);
break; break;
case FLOAT64: case FLOAT64:
if (expression.isReinterpret()) {
writer.writeByte(0xBF);
} else {
writer.writeByte(expression.isSigned() ? 0xB9 : 0xBA); writer.writeByte(expression.isSigned() ? 0xB9 : 0xBA);
}
break; break;
} }
break; break;
case FLOAT32: case FLOAT32:
switch (expression.getTargetType()) { switch (expression.getTargetType()) {
case INT32: case INT32:
if (expression.isReinterpret()) {
writer.writeByte(0xBC);
} else {
writer.writeByte(expression.isSigned() ? 0xA8 : 0xA9); writer.writeByte(expression.isSigned() ? 0xA8 : 0xA9);
}
break; break;
case INT64: case INT64:
writer.writeByte(expression.isSigned() ? 0xAE : 0xAF); writer.writeByte(expression.isSigned() ? 0xAE : 0xAF);
@ -625,7 +633,11 @@ class WasmBinaryRenderingVisitor implements WasmExpressionVisitor {
writer.writeByte(expression.isSigned() ? 0xAA : 0xAB); writer.writeByte(expression.isSigned() ? 0xAA : 0xAB);
break; break;
case INT64: case INT64:
if (expression.isReinterpret()) {
writer.writeByte(0xBD);
} else {
writer.writeByte(expression.isSigned() ? 0xB0 : 0xB1); writer.writeByte(expression.isSigned() ? 0xB0 : 0xB1);
}
break; break;
case FLOAT32: case FLOAT32:
writer.writeByte(0xB6); writer.writeByte(0xB6);

View File

@ -323,7 +323,11 @@ class WasmRenderingVisitor implements WasmExpressionVisitor {
break; break;
case FLOAT32: case FLOAT32:
case FLOAT64: case FLOAT64:
if (expression.isReinterpret()) {
name = "reinterpret";
} else {
name = expression.isSigned() ? "convert_s" : "convert_u"; name = expression.isSigned() ? "convert_s" : "convert_u";
}
break; break;
} }
break; break;
@ -336,7 +340,11 @@ class WasmRenderingVisitor implements WasmExpressionVisitor {
break; break;
case FLOAT32: case FLOAT32:
case FLOAT64: case FLOAT64:
if (expression.isReinterpret()) {
name = "reinterpret";
} else {
name = expression.isSigned() ? "convert_s" : "convert_u"; name = expression.isSigned() ? "convert_s" : "convert_u";
}
break; break;
} }
break; break;
@ -344,7 +352,11 @@ class WasmRenderingVisitor implements WasmExpressionVisitor {
switch (expression.getTargetType()) { switch (expression.getTargetType()) {
case INT32: case INT32:
case INT64: case INT64:
if (expression.isReinterpret()) {
name = "reinterpret";
} else {
name = expression.isSigned() ? "trunc_s" : "trunc_u"; name = expression.isSigned() ? "trunc_s" : "trunc_u";
}
break; break;
case FLOAT32: case FLOAT32:
break; break;
@ -357,7 +369,11 @@ class WasmRenderingVisitor implements WasmExpressionVisitor {
switch (expression.getTargetType()) { switch (expression.getTargetType()) {
case INT32: case INT32:
case INT64: case INT64:
if (expression.isReinterpret()) {
name = "reinterpret";
} else {
name = expression.isSigned() ? "trunc_s" : "trunc_u"; name = expression.isSigned() ? "trunc_s" : "trunc_u";
}
break; break;
case FLOAT32: case FLOAT32:
name = "demote"; name = "demote";