From 95afeb17a74fa5b2a914a6229ea94a53abc16137 Mon Sep 17 00:00:00 2001 From: Volker Date: Thu, 21 Jun 2018 18:29:20 +0200 Subject: [PATCH] Next step of refactoring with an intermediate model in the memory of WASM instructions. --- .../module/ValueTypeConvertion.java | 13 ++++- .../module/WasmBlockInstruction.java | 55 ++++++++++++++++++ .../module/WasmCallInstruction.java | 47 ++++++++++++++++ .../module/WasmConstInstruction.java | 4 +- .../jwebassembly/module/WasmInstruction.java | 2 +- .../module/WasmLoadStoreInstruction.java | 4 +- .../module/WasmNumericInstruction.java | 56 +++++++++++++++++++ 7 files changed, 175 insertions(+), 6 deletions(-) create mode 100644 src/de/inetsoftware/jwebassembly/module/WasmBlockInstruction.java create mode 100644 src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java create mode 100644 src/de/inetsoftware/jwebassembly/module/WasmNumericInstruction.java diff --git a/src/de/inetsoftware/jwebassembly/module/ValueTypeConvertion.java b/src/de/inetsoftware/jwebassembly/module/ValueTypeConvertion.java index 5609452..c14dd76 100644 --- a/src/de/inetsoftware/jwebassembly/module/ValueTypeConvertion.java +++ b/src/de/inetsoftware/jwebassembly/module/ValueTypeConvertion.java @@ -15,13 +15,15 @@ */ package de.inetsoftware.jwebassembly.module; +import java.io.IOException; + /** * Cast operations for converting one data type to another * * @author Volker Berlin * */ -public enum ValueTypeConvertion { +public enum ValueTypeConvertion implements WasmInstruction { i2l, i2f, i2d, @@ -34,4 +36,13 @@ public enum ValueTypeConvertion { d2i, d2l, d2f, + ; + + /** + * {@inheritDoc} + */ + @Override + public void writeTo( ModuleWriter writer ) throws IOException { + writer.writeCast( this ); + } } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmBlockInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmBlockInstruction.java new file mode 100644 index 0000000..b54d8df --- /dev/null +++ b/src/de/inetsoftware/jwebassembly/module/WasmBlockInstruction.java @@ -0,0 +1,55 @@ +/* + Copyright 2018 Volker Berlin (i-net software) + + 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 de.inetsoftware.jwebassembly.module; + +import java.io.IOException; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +/** + * WasmInstruction for block operation. + * + * @author Volker Berlin + * + */ +class WasmBlockInstruction implements WasmInstruction { + + private final WasmBlockOperator op; + + private final Object data; + + /** + * Create an instance of block operation. + * + * @param op + * the operation + * @param data + * extra data depending of the operator + */ + WasmBlockInstruction( @Nonnull WasmBlockOperator op, @Nullable Object data ) { + this.op = op; + this.data = data; + } + + /** + * {@inheritDoc} + */ + public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { + writer.writeBlockCode( op, data ); + } +} diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java new file mode 100644 index 0000000..cb1635e --- /dev/null +++ b/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java @@ -0,0 +1,47 @@ +/* + Copyright 2018 Volker Berlin (i-net software) + + 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 de.inetsoftware.jwebassembly.module; + +import java.io.IOException; + +import javax.annotation.Nonnull; + +/** + * WasmInstruction for a function call. + * + * @author Volker Berlin + * + */ +class WasmCallInstruction implements WasmInstruction { + + private final String name; + + /** + * Create an instance of a function call instruction + * @param name the Java function name + */ + WasmCallInstruction( String name ) { + this.name = name; + } + + /** + * {@inheritDoc} + */ + public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { + writer.writeFunctionCall( name ); + } +} diff --git a/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java index 74f6833..9340ff6 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java @@ -28,7 +28,7 @@ import de.inetsoftware.jwebassembly.WasmException; * @author Volker Berlin * */ -class WasmConstInstruction extends WasmInstruction { +class WasmConstInstruction implements WasmInstruction { private final Number value; @@ -43,7 +43,7 @@ class WasmConstInstruction extends WasmInstruction { /** * {@inheritDoc} */ - void writeTo( @Nonnull ModuleWriter writer ) throws IOException { + public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { Class clazz = value.getClass(); if( clazz == Integer.class ) { writer.writeConstInt( ((Integer)value).intValue() ); diff --git a/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java index 275b3e2..baae473 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java @@ -26,7 +26,7 @@ import javax.annotation.Nonnull; * @author Volker Berlin * */ -abstract class WasmInstruction { +abstract interface WasmInstruction { /** * Write this instruction to the WASM module. diff --git a/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java index a224843..0e298c0 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java @@ -27,7 +27,7 @@ import javax.annotation.Nonnull; * @author Volker Berlin * */ -class WasmLoadStoreInstruction extends WasmInstruction { +class WasmLoadStoreInstruction implements WasmInstruction { private boolean load; @@ -54,7 +54,7 @@ class WasmLoadStoreInstruction extends WasmInstruction { /** * {@inheritDoc} */ - void writeTo( @Nonnull ModuleWriter writer ) throws IOException { + public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { idx = localVariables.get( idx ); // translate slot index to position index if( load ) { writer.writeLoad( idx ); diff --git a/src/de/inetsoftware/jwebassembly/module/WasmNumericInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmNumericInstruction.java new file mode 100644 index 0000000..3e4c1eb --- /dev/null +++ b/src/de/inetsoftware/jwebassembly/module/WasmNumericInstruction.java @@ -0,0 +1,56 @@ +/* + Copyright 2018 Volker Berlin (i-net software) + + 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 de.inetsoftware.jwebassembly.module; + +import java.io.IOException; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +/** + * WasmInstruction for numeric operation. + * + * @author Volker Berlin + * + */ +class WasmNumericInstruction implements WasmInstruction { + + private final NumericOperator numOp; + + private final ValueType valueType; + + /** + * Create an instance of numeric operation. + * + * @param numOp + * the numeric operation + * @param valueType + * the type of the parameters + * + */ + WasmNumericInstruction( @Nullable NumericOperator numOp, @Nullable ValueType valueType ) { + this.numOp = numOp; + this.valueType = valueType; + } + + /** + * {@inheritDoc} + */ + public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { + writer.writeNumericOperator( numOp, valueType ); + } +}