diff --git a/core/src/main/java/org/teavm/backend/wasm/intrinsics/LongIntrinsic.java b/core/src/main/java/org/teavm/backend/wasm/intrinsics/LongIntrinsic.java index 43ad044aa..f1f9c662f 100644 --- a/core/src/main/java/org/teavm/backend/wasm/intrinsics/LongIntrinsic.java +++ b/core/src/main/java/org/teavm/backend/wasm/intrinsics/LongIntrinsic.java @@ -17,7 +17,9 @@ package org.teavm.backend.wasm.intrinsics; import org.teavm.ast.InvocationExpr; import org.teavm.backend.wasm.WasmRuntime; +import org.teavm.backend.wasm.model.WasmNumType; import org.teavm.backend.wasm.model.expression.WasmCall; +import org.teavm.backend.wasm.model.expression.WasmConversion; import org.teavm.backend.wasm.model.expression.WasmExpression; import org.teavm.backend.wasm.model.expression.WasmIntBinary; import org.teavm.backend.wasm.model.expression.WasmIntBinaryOperation; @@ -65,16 +67,20 @@ public class LongIntrinsic implements WasmIntrinsic { manager.generate(invocation.getArguments().get(0)), manager.generate(invocation.getArguments().get(1))); case "numberOfLeadingZeros": - return new WasmIntUnary(WasmIntType.INT64, WasmIntUnaryOperation.CLZ, - manager.generate(invocation.getArguments().get(0))); + return castToInt(new WasmIntUnary(WasmIntType.INT64, WasmIntUnaryOperation.CLZ, + manager.generate(invocation.getArguments().get(0)))); case "numberOfTrailingZeros": - return new WasmIntUnary(WasmIntType.INT64, WasmIntUnaryOperation.CTZ, - manager.generate(invocation.getArguments().get(0))); + return castToInt(new WasmIntUnary(WasmIntType.INT64, WasmIntUnaryOperation.CTZ, + manager.generate(invocation.getArguments().get(0)))); case "bitCount": - return new WasmIntUnary(WasmIntType.INT64, WasmIntUnaryOperation.POPCNT, - manager.generate(invocation.getArguments().get(0))); + return castToInt(new WasmIntUnary(WasmIntType.INT64, WasmIntUnaryOperation.POPCNT, + manager.generate(invocation.getArguments().get(0)))); default: throw new AssertionError(); } } + + private WasmExpression castToInt(WasmExpression expr) { + return new WasmConversion(WasmNumType.INT64, WasmNumType.INT32, false, expr); + } } diff --git a/core/src/main/java/org/teavm/backend/wasm/intrinsics/gc/IntNumIntrinsic.java b/core/src/main/java/org/teavm/backend/wasm/intrinsics/gc/IntNumIntrinsic.java index db95b6189..4dbed27aa 100644 --- a/core/src/main/java/org/teavm/backend/wasm/intrinsics/gc/IntNumIntrinsic.java +++ b/core/src/main/java/org/teavm/backend/wasm/intrinsics/gc/IntNumIntrinsic.java @@ -17,7 +17,9 @@ package org.teavm.backend.wasm.intrinsics.gc; import org.teavm.ast.InvocationExpr; import org.teavm.backend.wasm.WasmRuntime; +import org.teavm.backend.wasm.model.WasmNumType; import org.teavm.backend.wasm.model.expression.WasmCall; +import org.teavm.backend.wasm.model.expression.WasmConversion; import org.teavm.backend.wasm.model.expression.WasmExpression; import org.teavm.backend.wasm.model.expression.WasmIntBinary; import org.teavm.backend.wasm.model.expression.WasmIntBinaryOperation; @@ -51,16 +53,24 @@ public class IntNumIntrinsic implements WasmGCIntrinsic { context.generate(invocation.getArguments().get(0)), context.generate(invocation.getArguments().get(1))); case "numberOfLeadingZeros": - return new WasmIntUnary(wasmType, WasmIntUnaryOperation.CLZ, - context.generate(invocation.getArguments().get(0))); + return castToInt(new WasmIntUnary(wasmType, WasmIntUnaryOperation.CLZ, + context.generate(invocation.getArguments().get(0)))); case "numberOfTrailingZeros": - return new WasmIntUnary(wasmType, WasmIntUnaryOperation.CTZ, - context.generate(invocation.getArguments().get(0))); + return castToInt(new WasmIntUnary(wasmType, WasmIntUnaryOperation.CTZ, + context.generate(invocation.getArguments().get(0)))); case "bitCount": - return new WasmIntUnary(wasmType, WasmIntUnaryOperation.POPCNT, - context.generate(invocation.getArguments().get(0))); + return castToInt(new WasmIntUnary(wasmType, WasmIntUnaryOperation.POPCNT, + context.generate(invocation.getArguments().get(0)))); default: throw new AssertionError(); } } + + private WasmExpression castToInt(WasmExpression expr) { + if (wasmType == WasmIntType.INT64) { + return new WasmConversion(WasmNumType.INT64, WasmNumType.INT32, false, expr); + } else { + return expr; + } + } }