From 5fb83ca2acba45e3dcafdf78a9fd2567ddc8fccb Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 11 Nov 2022 09:38:21 +0100 Subject: [PATCH] Remove unnecessary Wasm runtime imports, implement isFinite method --- .../backend/wasm/generate/WasmGenerationVisitor.java | 10 +++++----- .../backend/wasm/intrinsics/DoubleIntrinsic.java | 10 +++++++++- .../backend/wasm/intrinsics/FloatIntrinsic.java | 8 ++++++++ .../resources/org/teavm/backend/wasm/wasm-runtime.c | 3 --- .../resources/org/teavm/backend/wasm/wasm-runtime.js | 12 ------------ 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/core/src/main/java/org/teavm/backend/wasm/generate/WasmGenerationVisitor.java b/core/src/main/java/org/teavm/backend/wasm/generate/WasmGenerationVisitor.java index 270d27af1..a4f4d8b7b 100644 --- a/core/src/main/java/org/teavm/backend/wasm/generate/WasmGenerationVisitor.java +++ b/core/src/main/java/org/teavm/backend/wasm/generate/WasmGenerationVisitor.java @@ -1451,7 +1451,7 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor { expr.getIndex().acceptVisitor(this); } - private WasmExpression negate(WasmExpression expr) { + private static WasmExpression negate(WasmExpression expr) { if (expr instanceof WasmIntBinary) { WasmIntBinary binary = (WasmIntBinary) expr; if (binary.getType() == WasmIntType.INT32 && binary.getOperation() == WasmIntBinaryOperation.XOR) { @@ -1478,11 +1478,11 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor { return new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.EQ, expr, new WasmInt32Constant(0)); } - private boolean isOne(WasmExpression expression) { + private static boolean isOne(WasmExpression expression) { return expression instanceof WasmInt32Constant && ((WasmInt32Constant) expression).getValue() == 1; } - private boolean isZero(WasmExpression expression) { + private static boolean isZero(WasmExpression expression) { return expression instanceof WasmInt32Constant && ((WasmInt32Constant) expression).getValue() == 0; } @@ -1546,7 +1546,7 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor { return expression; } - private WasmIntBinaryOperation negate(WasmIntBinaryOperation op) { + private static WasmIntBinaryOperation negate(WasmIntBinaryOperation op) { switch (op) { case EQ: return WasmIntBinaryOperation.NE; @@ -1573,7 +1573,7 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor { } } - private WasmFloatBinaryOperation negate(WasmFloatBinaryOperation op) { + private static WasmFloatBinaryOperation negate(WasmFloatBinaryOperation op) { switch (op) { case EQ: return WasmFloatBinaryOperation.NE; diff --git a/core/src/main/java/org/teavm/backend/wasm/intrinsics/DoubleIntrinsic.java b/core/src/main/java/org/teavm/backend/wasm/intrinsics/DoubleIntrinsic.java index e6756d460..b4d768ef2 100644 --- a/core/src/main/java/org/teavm/backend/wasm/intrinsics/DoubleIntrinsic.java +++ b/core/src/main/java/org/teavm/backend/wasm/intrinsics/DoubleIntrinsic.java @@ -47,6 +47,7 @@ public class DoubleIntrinsic implements WasmIntrinsic { case "getNaN": case "isNaN": case "isInfinite": + case "isFinite": case "doubleToLongBits": case "longBitsToDouble": return true; @@ -65,7 +66,14 @@ public class DoubleIntrinsic implements WasmIntrinsic { WasmIntBinaryOperation.NE); case "isInfinite": return testSpecialIEEE(manager.generate(invocation.getArguments().get(0)), manager, - WasmIntBinaryOperation.EQ); + WasmIntBinaryOperation.NE); + case "isFinite": { + WasmExpression result = testSpecialIEEE(manager.generate(invocation.getArguments().get(0)), manager, + WasmIntBinaryOperation.NE); + result = new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.EQ, result, + new WasmInt32Constant(0)); + return result; + } case "doubleToLongBits": { WasmConversion conversion = new WasmConversion(WasmType.FLOAT64, WasmType.INT64, false, manager.generate(invocation.getArguments().get(0))); diff --git a/core/src/main/java/org/teavm/backend/wasm/intrinsics/FloatIntrinsic.java b/core/src/main/java/org/teavm/backend/wasm/intrinsics/FloatIntrinsic.java index bf7c4076e..f897ec4f2 100644 --- a/core/src/main/java/org/teavm/backend/wasm/intrinsics/FloatIntrinsic.java +++ b/core/src/main/java/org/teavm/backend/wasm/intrinsics/FloatIntrinsic.java @@ -46,6 +46,7 @@ public class FloatIntrinsic implements WasmIntrinsic { case "getNaN": case "isNaN": case "isInfinite": + case "isFinite": case "floatToIntBits": case "intBitsToFloat": return true; @@ -65,6 +66,13 @@ public class FloatIntrinsic implements WasmIntrinsic { case "isInfinite": return testSpecialIEEE(manager.generate(invocation.getArguments().get(0)), manager, WasmIntBinaryOperation.EQ); + case "isFinite": { + WasmExpression result = testSpecialIEEE(manager.generate(invocation.getArguments().get(0)), manager, + WasmIntBinaryOperation.EQ); + result = new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.EQ, result, + new WasmInt32Constant(0)); + return result; + } case "floatToIntBits": { WasmConversion conversion = new WasmConversion(WasmType.FLOAT32, WasmType.INT32, false, manager.generate(invocation.getArguments().get(0))); diff --git a/core/src/main/resources/org/teavm/backend/wasm/wasm-runtime.c b/core/src/main/resources/org/teavm/backend/wasm/wasm-runtime.c index 647307fc1..3b7da14a9 100644 --- a/core/src/main/resources/org/teavm/backend/wasm/wasm-runtime.c +++ b/core/src/main/resources/org/teavm/backend/wasm/wasm-runtime.c @@ -16,9 +16,6 @@ float teavm_teavm_getNaN() { return NAN; } -#define teavm_isnan isnan -#define teavm_isinf isinf -#define teavm_isfinite isfinite #define teavmMath_sin sin #define teavmMath_cos cos #define teavmMath_sqrt sqrt diff --git a/core/src/main/resources/org/teavm/backend/wasm/wasm-runtime.js b/core/src/main/resources/org/teavm/backend/wasm/wasm-runtime.js index d1ca9edad..f6b938ba2 100644 --- a/core/src/main/resources/org/teavm/backend/wasm/wasm-runtime.js +++ b/core/src/main/resources/org/teavm/backend/wasm/wasm-runtime.js @@ -39,12 +39,6 @@ TeaVM.wasm = function() { putwchar(memory[buffer++]); } } - function towlower(code) { - return String.fromCharCode(code).toLowerCase().charCodeAt(0); - } - function towupper(code) { - return String.fromCharCode(code).toUpperCase().charCodeAt(0); - } function currentTimeMillis() { return new Date().getTime(); } @@ -94,14 +88,8 @@ TeaVM.wasm = function() { obj.teavm = { currentTimeMillis: currentTimeMillis, nanoTime: function() { return performance.now(); }, - isnan: isNaN, - teavm_getNaN: function() { return NaN; }, - isinf: function(n) { return !isFinite(n) }, - isfinite: isFinite, putwcharsOut: (chars, count) => putwchars(controller, chars, count), putwcharsErr: (chars, count) => putwchars(controller, chars, count), - towlower: towlower, - towupper: towupper, getNativeOffset: getNativeOffset, logString: string => logString(string, controller), logInt: logInt,