mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-25 15:37:52 +01:00
more math operation for the text format (Watparser)
This commit is contained in:
parent
0a789fd40e
commit
c64c620a38
@ -758,17 +758,6 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case sqrt:
|
|
||||||
switch( valueType ) {
|
|
||||||
case f32:
|
|
||||||
op = F32_SQRT;
|
|
||||||
break;
|
|
||||||
case f64:
|
|
||||||
op = F64_SQRT;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case rem:
|
case rem:
|
||||||
switch( valueType ) {
|
switch( valueType ) {
|
||||||
case i32:
|
case i32:
|
||||||
@ -973,6 +962,62 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
|||||||
codeStream.writeOpCode( REF_EQ );
|
codeStream.writeOpCode( REF_EQ );
|
||||||
op = I32_EQZ;
|
op = I32_EQZ;
|
||||||
break;
|
break;
|
||||||
|
case sqrt:
|
||||||
|
switch( valueType ) {
|
||||||
|
case f32:
|
||||||
|
op = F32_SQRT;
|
||||||
|
break;
|
||||||
|
case f64:
|
||||||
|
op = F64_SQRT;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ceil:
|
||||||
|
switch( valueType ) {
|
||||||
|
case f32:
|
||||||
|
op = F32_CEIL;
|
||||||
|
break;
|
||||||
|
case f64:
|
||||||
|
op = F64_CEIL;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case floor:
|
||||||
|
switch( valueType ) {
|
||||||
|
case f32:
|
||||||
|
op = F32_FLOOR;
|
||||||
|
break;
|
||||||
|
case f64:
|
||||||
|
op = F64_FLOOR;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case trunc:
|
||||||
|
switch( valueType ) {
|
||||||
|
case f32:
|
||||||
|
op = F32_TRUNC;
|
||||||
|
break;
|
||||||
|
case f64:
|
||||||
|
op = F64_TRUNC;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case nearest:
|
||||||
|
switch( valueType ) {
|
||||||
|
case f32:
|
||||||
|
op = F32_NEAREST;
|
||||||
|
break;
|
||||||
|
case f64:
|
||||||
|
op = F64_NEAREST;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
if( op == 0 ) {
|
if( op == 0 ) {
|
||||||
|
@ -81,7 +81,7 @@ class WasmNumericInstruction extends WasmInstruction {
|
|||||||
case lt:
|
case lt:
|
||||||
case le:
|
case le:
|
||||||
case ge:
|
case ge:
|
||||||
return null;
|
return null; //TODO should this Valuetype.i32? But then tests failed. can be related to the getPopCount() of the block BlockInstaction IF
|
||||||
default:
|
default:
|
||||||
return valueType;
|
return valueType;
|
||||||
}
|
}
|
||||||
@ -94,6 +94,11 @@ class WasmNumericInstruction extends WasmInstruction {
|
|||||||
int getPopCount() {
|
int getPopCount() {
|
||||||
switch( numOp ) {
|
switch( numOp ) {
|
||||||
case neg:
|
case neg:
|
||||||
|
case sqrt:
|
||||||
|
case ceil:
|
||||||
|
case floor:
|
||||||
|
case trunc:
|
||||||
|
case nearest:
|
||||||
return 1;
|
return 1;
|
||||||
default:
|
default:
|
||||||
return 2;
|
return 2;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2017 - 2018 Volker Berlin (i-net software)
|
* Copyright 2017 - 2019 Volker Berlin (i-net software)
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -24,7 +24,6 @@ public enum NumericOperator {
|
|||||||
neg,
|
neg,
|
||||||
mul,
|
mul,
|
||||||
div,
|
div,
|
||||||
sqrt,
|
|
||||||
rem,
|
rem,
|
||||||
and,
|
and,
|
||||||
or,
|
or,
|
||||||
@ -44,4 +43,9 @@ public enum NumericOperator {
|
|||||||
ifnonnull,
|
ifnonnull,
|
||||||
ref_eq,
|
ref_eq,
|
||||||
ref_ne,
|
ref_ne,
|
||||||
|
sqrt,
|
||||||
|
ceil,
|
||||||
|
floor,
|
||||||
|
trunc,
|
||||||
|
nearest,
|
||||||
}
|
}
|
||||||
|
@ -78,42 +78,66 @@ public class WatParser extends WasmCodeBuilder {
|
|||||||
case "i64.trunc_sat_f64_s":
|
case "i64.trunc_sat_f64_s":
|
||||||
addConvertInstruction( ValueTypeConvertion.d2l, javaCodePos, lineNumber );
|
addConvertInstruction( ValueTypeConvertion.d2l, javaCodePos, lineNumber );
|
||||||
break;
|
break;
|
||||||
|
case "f32.ceil":
|
||||||
|
addNumericInstruction( NumericOperator.ceil, ValueType.f32, javaCodePos, lineNumber );
|
||||||
|
break;
|
||||||
case "f32.convert_i32_s":
|
case "f32.convert_i32_s":
|
||||||
addConvertInstruction( ValueTypeConvertion.i2f, javaCodePos, lineNumber );
|
addConvertInstruction( ValueTypeConvertion.i2f, javaCodePos, lineNumber );
|
||||||
break;
|
break;
|
||||||
case "f32.div":
|
case "f32.div":
|
||||||
addNumericInstruction( NumericOperator.div, ValueType.f32, javaCodePos, lineNumber );
|
addNumericInstruction( NumericOperator.div, ValueType.f32, javaCodePos, lineNumber );
|
||||||
break;
|
break;
|
||||||
|
case "f32.floor":
|
||||||
|
addNumericInstruction( NumericOperator.floor, ValueType.f32, javaCodePos, lineNumber );
|
||||||
|
break;
|
||||||
case "f32.max":
|
case "f32.max":
|
||||||
addNumericInstruction( NumericOperator.max, ValueType.f32, javaCodePos, lineNumber );
|
addNumericInstruction( NumericOperator.max, ValueType.f32, javaCodePos, lineNumber );
|
||||||
break;
|
break;
|
||||||
case "f32.mul":
|
case "f32.mul":
|
||||||
addNumericInstruction( NumericOperator.mul, ValueType.f32, javaCodePos, lineNumber );
|
addNumericInstruction( NumericOperator.mul, ValueType.f32, javaCodePos, lineNumber );
|
||||||
break;
|
break;
|
||||||
|
case "f32.nearest":
|
||||||
|
addNumericInstruction( NumericOperator.nearest, ValueType.f32, javaCodePos, lineNumber );
|
||||||
|
break;
|
||||||
case "f32.sqrt":
|
case "f32.sqrt":
|
||||||
addNumericInstruction( NumericOperator.sqrt, ValueType.f32, javaCodePos, lineNumber );
|
addNumericInstruction( NumericOperator.sqrt, ValueType.f32, javaCodePos, lineNumber );
|
||||||
break;
|
break;
|
||||||
case "f32.sub":
|
case "f32.sub":
|
||||||
addNumericInstruction( NumericOperator.sub, ValueType.f32, javaCodePos, lineNumber );
|
addNumericInstruction( NumericOperator.sub, ValueType.f32, javaCodePos, lineNumber );
|
||||||
break;
|
break;
|
||||||
|
case "f32.trunc":
|
||||||
|
addNumericInstruction( NumericOperator.trunc, ValueType.f32, javaCodePos, lineNumber );
|
||||||
|
break;
|
||||||
|
case "f64.ceil":
|
||||||
|
addNumericInstruction( NumericOperator.ceil, ValueType.f64, javaCodePos, lineNumber );
|
||||||
|
break;
|
||||||
case "f64.convert_i64_s":
|
case "f64.convert_i64_s":
|
||||||
addConvertInstruction( ValueTypeConvertion.l2d, javaCodePos, lineNumber );
|
addConvertInstruction( ValueTypeConvertion.l2d, javaCodePos, lineNumber );
|
||||||
break;
|
break;
|
||||||
case "f64.div":
|
case "f64.div":
|
||||||
addNumericInstruction( NumericOperator.div, ValueType.f64, javaCodePos, lineNumber );
|
addNumericInstruction( NumericOperator.div, ValueType.f64, javaCodePos, lineNumber );
|
||||||
break;
|
break;
|
||||||
|
case "f64.floor":
|
||||||
|
addNumericInstruction( NumericOperator.floor, ValueType.f64, javaCodePos, lineNumber );
|
||||||
|
break;
|
||||||
case "f64.max":
|
case "f64.max":
|
||||||
addNumericInstruction( NumericOperator.max, ValueType.f64, javaCodePos, lineNumber );
|
addNumericInstruction( NumericOperator.max, ValueType.f64, javaCodePos, lineNumber );
|
||||||
break;
|
break;
|
||||||
case "f64.mul":
|
case "f64.mul":
|
||||||
addNumericInstruction( NumericOperator.mul, ValueType.f64, javaCodePos, lineNumber );
|
addNumericInstruction( NumericOperator.mul, ValueType.f64, javaCodePos, lineNumber );
|
||||||
break;
|
break;
|
||||||
|
case "f64.nearest":
|
||||||
|
addNumericInstruction( NumericOperator.nearest, ValueType.f64, javaCodePos, lineNumber );
|
||||||
|
break;
|
||||||
case "f64.sqrt":
|
case "f64.sqrt":
|
||||||
addNumericInstruction( NumericOperator.sqrt, ValueType.f64, javaCodePos, lineNumber );
|
addNumericInstruction( NumericOperator.sqrt, ValueType.f64, javaCodePos, lineNumber );
|
||||||
break;
|
break;
|
||||||
case "f64.sub":
|
case "f64.sub":
|
||||||
addNumericInstruction( NumericOperator.sub, ValueType.f64, javaCodePos, lineNumber );
|
addNumericInstruction( NumericOperator.sub, ValueType.f64, javaCodePos, lineNumber );
|
||||||
break;
|
break;
|
||||||
|
case "f64.trunc":
|
||||||
|
addNumericInstruction( NumericOperator.trunc, ValueType.f64, javaCodePos, lineNumber );
|
||||||
|
break;
|
||||||
// case "call":
|
// case "call":
|
||||||
// addCallInstruction( method, javaCodePos );
|
// addCallInstruction( method, javaCodePos );
|
||||||
// break;
|
// break;
|
||||||
|
@ -26,6 +26,7 @@ import javax.annotation.Nullable;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import de.inetsoftware.jwebassembly.WasmException;
|
import de.inetsoftware.jwebassembly.WasmException;
|
||||||
|
import de.inetsoftware.jwebassembly.binary.BinaryModuleWriter;
|
||||||
import de.inetsoftware.jwebassembly.text.TextModuleWriter;
|
import de.inetsoftware.jwebassembly.text.TextModuleWriter;
|
||||||
import de.inetsoftware.jwebassembly.watparser.WatParser;
|
import de.inetsoftware.jwebassembly.watparser.WatParser;
|
||||||
|
|
||||||
@ -49,6 +50,13 @@ public class WatParserTest {
|
|||||||
String expected = normalize( "(module (func $A.a " + wat + " ) )" );
|
String expected = normalize( "(module (func $A.a " + wat + " ) )" );
|
||||||
String actual = normalize( builder );
|
String actual = normalize( builder );
|
||||||
assertEquals( expected, actual );
|
assertEquals( expected, actual );
|
||||||
|
|
||||||
|
// smoke test of the binary writer
|
||||||
|
writer = new BinaryModuleWriter( new WasmTarget( builder ), new HashMap<>() );
|
||||||
|
writer.writeMethodStart( new FunctionName( "A.a()V" ), null );
|
||||||
|
for( WasmInstruction instruction : codeBuilder.getInstructions() ) {
|
||||||
|
instruction.writeTo( writer );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String normalize( @Nullable CharSequence str ) {
|
private String normalize( @Nullable CharSequence str ) {
|
||||||
@ -123,6 +131,11 @@ public class WatParserTest {
|
|||||||
test( "i64.trunc_sat_f64_s" );
|
test( "i64.trunc_sat_f64_s" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void f32_ceil() throws IOException {
|
||||||
|
test( "f32.ceil" );
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void f32_convert_i32_s() throws IOException {
|
public void f32_convert_i32_s() throws IOException {
|
||||||
test( "f32.convert_i32_s" );
|
test( "f32.convert_i32_s" );
|
||||||
@ -133,6 +146,11 @@ public class WatParserTest {
|
|||||||
test( "f32.div" );
|
test( "f32.div" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void f32_floor() throws IOException {
|
||||||
|
test( "f32.floor" );
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void f32_max() throws IOException {
|
public void f32_max() throws IOException {
|
||||||
test( "f32.max" );
|
test( "f32.max" );
|
||||||
@ -143,11 +161,31 @@ public class WatParserTest {
|
|||||||
test( "f32.mul" );
|
test( "f32.mul" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void f32_nearest() throws IOException {
|
||||||
|
test( "f32.nearest" );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void f32_sqrt() throws IOException {
|
||||||
|
test( "f32.sqrt" );
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void f32_sub() throws IOException {
|
public void f32_sub() throws IOException {
|
||||||
test( "f32.sub" );
|
test( "f32.sub" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void f32_trunc() throws IOException {
|
||||||
|
test( "f32.trunc" );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void f64_ceil() throws IOException {
|
||||||
|
test( "f64.ceil" );
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void f64_convert_i64_s() throws IOException {
|
public void f64_convert_i64_s() throws IOException {
|
||||||
test( "f64.convert_i64_s" );
|
test( "f64.convert_i64_s" );
|
||||||
@ -158,6 +196,16 @@ public class WatParserTest {
|
|||||||
test( "f64.div" );
|
test( "f64.div" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void f64_floor() throws IOException {
|
||||||
|
test( "f64.floor" );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void f64_nearest() throws IOException {
|
||||||
|
test( "f64.nearest" );
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void f64_max() throws IOException {
|
public void f64_max() throws IOException {
|
||||||
test( "f64.max" );
|
test( "f64.max" );
|
||||||
@ -168,11 +216,21 @@ public class WatParserTest {
|
|||||||
test( "f64.mul" );
|
test( "f64.mul" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void f64_sqrt() throws IOException {
|
||||||
|
test( "f64.sqrt" );
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void f64_sub() throws IOException {
|
public void f64_sub() throws IOException {
|
||||||
test( "f64.sub" );
|
test( "f64.sub" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void f64_trunc() throws IOException {
|
||||||
|
test( "f64.trunc" );
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void return_() throws IOException {
|
public void return_() throws IOException {
|
||||||
test( "return\n" );
|
test( "return\n" );
|
||||||
|
Loading…
x
Reference in New Issue
Block a user