From c2be64a5259b237dad0b642ee214fc6f566a1c82 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Tue, 12 Mar 2019 21:27:23 +0100 Subject: [PATCH] implements java bytecode frem and drem --- .../module/JavaMethodWasmCodeBuilder.java | 8 ++++++-- .../jwebassembly/module/SyntheticFunctionName.java | 2 +- .../jwebassembly/runtime/MathOperations.java | 2 ++ .../jwebassembly/runtime/RuntimeErrors.java | 12 ------------ 4 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java index 157f8b7..53af49c 100644 --- a/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java @@ -342,9 +342,13 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder { addNumericInstruction( NumericOperator.rem, ValueType.i64, codePos ); break; case 114: // frem + //helper function like: (a - (int)(a / b) * (float)b) + addCallInstruction( new SyntheticFunctionName( "frem", "local.get 0 local.get 0 local.get 1 f32.div i32.trunc_sat_f32_s f32.convert_i32_s local.get 1 f32.mul f32.sub return", ValueType.f32, ValueType.f32, null, ValueType.f32 ), codePos ); + break; case 115: // drem - //TODO can be implemented with a helper function like: (a - (long)(a / b) * (double)b) - throw new WasmException( "Modulo/Remainder for floating numbers is not supported in WASM. Use int or long data types." + op, byteCode.getLineNumber() ); + //helper function like: (a - (long)(a / b) * (double)b) + addCallInstruction( new SyntheticFunctionName( "drem", "local.get 0 local.get 0 local.get 1 f64.div i64.trunc_sat_f64_s f64.convert_i64_s local.get 1 f64.mul f64.sub return", ValueType.f64, ValueType.f64, null, ValueType.f64 ), codePos ); + break; case 116: // ineg addConstInstruction( -1, ValueType.i32, codePos ); addNumericInstruction( NumericOperator.mul, ValueType.i32, codePos ); diff --git a/src/de/inetsoftware/jwebassembly/module/SyntheticFunctionName.java b/src/de/inetsoftware/jwebassembly/module/SyntheticFunctionName.java index 497430b..19f2988 100644 --- a/src/de/inetsoftware/jwebassembly/module/SyntheticFunctionName.java +++ b/src/de/inetsoftware/jwebassembly/module/SyntheticFunctionName.java @@ -40,7 +40,7 @@ public class SyntheticFunctionName extends FunctionName { * @param code * the WAT code (WASM in text form) * @param signature - * the method signature + * the method signature, first the parameters, then null and the the return types */ public SyntheticFunctionName( String name, String code, AnyType... signature ) { super( "", name, "()V" ); //TODO better signature name diff --git a/test/de/inetsoftware/jwebassembly/runtime/MathOperations.java b/test/de/inetsoftware/jwebassembly/runtime/MathOperations.java index f1dca73..e14c24a 100644 --- a/test/de/inetsoftware/jwebassembly/runtime/MathOperations.java +++ b/test/de/inetsoftware/jwebassembly/runtime/MathOperations.java @@ -181,6 +181,7 @@ public class MathOperations extends AbstractBaseTest { float a = -54321F; a *= 3F; a /= -8F; + a %= 37.5F; return a; } @@ -189,6 +190,7 @@ public class MathOperations extends AbstractBaseTest { double a = -54321.0; a *= 3F; a /= -5F; + a %= 37.5; return a; } diff --git a/test/de/inetsoftware/jwebassembly/runtime/RuntimeErrors.java b/test/de/inetsoftware/jwebassembly/runtime/RuntimeErrors.java index d0ef899..0df5b6f 100644 --- a/test/de/inetsoftware/jwebassembly/runtime/RuntimeErrors.java +++ b/test/de/inetsoftware/jwebassembly/runtime/RuntimeErrors.java @@ -85,18 +85,6 @@ public class RuntimeErrors { } } - @Test - public void floatRem() throws IOException { - compileErrorTest( "Modulo/Remainder", TestModulo.class ); - } - - static class TestModulo { - @Export - static float longReturn() { - float a = 3.4F; - return a % 2F; - } - } @Test public void nonStaticExport() throws IOException {