From bc2975683af36098d809a89ad8f190ce03a80da8 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Sun, 16 Dec 2018 18:22:44 +0100 Subject: [PATCH] add getType() to WasmInstruction --- .../jwebassembly/module/ModuleGenerator.java | 7 +++++-- .../jwebassembly/module/WasmArrayInstruction.java | 9 +++++++++ .../jwebassembly/module/WasmBlockInstruction.java | 9 +++++++++ .../jwebassembly/module/WasmCallInstruction.java | 8 ++++++++ .../jwebassembly/module/WasmConstInstruction.java | 9 +++++++++ .../module/WasmConvertInstruction.java | 9 +++++++++ .../jwebassembly/module/WasmGlobalInstruction.java | 9 +++++++++ .../jwebassembly/module/WasmInstruction.java | 14 ++++++++++++++ .../module/WasmLoadStoreInstruction.java | 9 +++++++++ .../jwebassembly/module/WasmLocalInstruction.java | 9 +++++++++ .../jwebassembly/module/WasmNopInstruction.java | 9 +++++++++ .../module/WasmNumericInstruction.java | 9 +++++++++ .../jwebassembly/module/WasmStructInstruction.java | 9 +++++++++ 13 files changed, 117 insertions(+), 2 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index 0eb4ad1..9f06acf 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -233,8 +233,11 @@ public class ModuleGenerator { writeMethodSignature( name, method.isStatic(), localVariableTable, codeBuilder ); for( WasmInstruction instruction : codeBuilder.getInstructions() ) { - if( instruction instanceof WasmCallInstruction ) { - functions.functionCall( ((WasmCallInstruction)instruction).getFunctionName() ); + switch( instruction.getType() ) { + case Call: + functions.functionCall( ((WasmCallInstruction)instruction).getFunctionName() ); + break; + default: } instruction.writeTo( writer ); } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmArrayInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmArrayInstruction.java index f583cd8..c5dda1a 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmArrayInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmArrayInstruction.java @@ -22,6 +22,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import de.inetsoftware.jwebassembly.WasmException; +import de.inetsoftware.jwebassembly.module.WasmInstruction.Type; import de.inetsoftware.jwebassembly.wasm.ArrayOperator; import de.inetsoftware.jwebassembly.wasm.StorageType; import de.inetsoftware.jwebassembly.wasm.ValueType; @@ -54,6 +55,14 @@ class WasmArrayInstruction extends WasmInstruction { this.type = type; } + /** + * {@inheritDoc} + */ + @Override + Type getType() { + return Type.Array; + } + /** * {@inheritDoc} */ diff --git a/src/de/inetsoftware/jwebassembly/module/WasmBlockInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmBlockInstruction.java index 4037e5f..d834844 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmBlockInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmBlockInstruction.java @@ -21,6 +21,7 @@ import java.io.IOException; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import de.inetsoftware.jwebassembly.module.WasmInstruction.Type; import de.inetsoftware.jwebassembly.wasm.ValueType; import de.inetsoftware.jwebassembly.wasm.WasmBlockOperator; @@ -52,6 +53,14 @@ class WasmBlockInstruction extends WasmInstruction { this.data = data; } + /** + * {@inheritDoc} + */ + @Override + Type getType() { + return Type.Block; + } + /** * {@inheritDoc} */ diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java index a5273e0..e9783ab 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java @@ -54,6 +54,14 @@ class WasmCallInstruction extends WasmInstruction { this.name = new FunctionName( method ); } + /** + * {@inheritDoc} + */ + @Override + Type getType() { + return Type.Call; + } + /** * Get the function name that should be called * diff --git a/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java index 1f09af5..1a80802 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java @@ -21,6 +21,7 @@ import java.io.IOException; import javax.annotation.Nonnull; import de.inetsoftware.jwebassembly.WasmException; +import de.inetsoftware.jwebassembly.module.WasmInstruction.Type; import de.inetsoftware.jwebassembly.wasm.ValueType; /** @@ -63,6 +64,14 @@ class WasmConstInstruction extends WasmInstruction { this( value, getValueType( value ), javaCodePos ); } + /** + * {@inheritDoc} + */ + @Override + Type getType() { + return Type.Const; + } + /** * Find the matching ValueType for the given value. * diff --git a/src/de/inetsoftware/jwebassembly/module/WasmConvertInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmConvertInstruction.java index 5e681ae..ba2549c 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmConvertInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmConvertInstruction.java @@ -17,6 +17,7 @@ package de.inetsoftware.jwebassembly.module; import java.io.IOException; +import de.inetsoftware.jwebassembly.module.WasmInstruction.Type; import de.inetsoftware.jwebassembly.wasm.ValueType; /** @@ -42,6 +43,14 @@ class WasmConvertInstruction extends WasmInstruction { this.conversion = conversion; } + /** + * {@inheritDoc} + */ + @Override + Type getType() { + return Type.Convert; + } + /** * {@inheritDoc} */ diff --git a/src/de/inetsoftware/jwebassembly/module/WasmGlobalInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmGlobalInstruction.java index 71e70c2..dc849b4 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmGlobalInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmGlobalInstruction.java @@ -21,6 +21,7 @@ import java.io.IOException; import javax.annotation.Nonnull; import de.inetsoftware.classparser.Member; +import de.inetsoftware.jwebassembly.module.WasmInstruction.Type; import de.inetsoftware.jwebassembly.wasm.ValueType; /** @@ -51,6 +52,14 @@ class WasmGlobalInstruction extends WasmInstruction { this.ref = ref; } + /** + * {@inheritDoc} + */ + @Override + Type getType() { + return Type.Global; + } + /** * {@inheritDoc} */ diff --git a/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java index c69731e..2eccaa8 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java @@ -31,6 +31,13 @@ import de.inetsoftware.jwebassembly.wasm.ValueType; */ abstract class WasmInstruction { + /** + * Type of instruction to faster differ as with instanceof. + */ + static enum Type { + LoadStore, Const, Convert, Local, Global, Block, Numeric, Nop, Call, Array, Struct; + } + private int javaCodePos; /** @@ -43,6 +50,13 @@ abstract class WasmInstruction { this.javaCodePos = javaCodePos; } + /** + * Get the type of instruction + * @return the type + */ + @Nonnull + abstract Type getType(); + /** * 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 e9761b6..519b776 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java @@ -21,6 +21,7 @@ import java.io.IOException; import javax.annotation.Nonnegative; import javax.annotation.Nonnull; +import de.inetsoftware.jwebassembly.module.WasmInstruction.Type; import de.inetsoftware.jwebassembly.wasm.ValueType; /** @@ -56,6 +57,14 @@ class WasmLoadStoreInstruction extends WasmInstruction { this.localVariables = localVariables; } + /** + * {@inheritDoc} + */ + @Override + Type getType() { + return Type.LoadStore; + } + /** * {@inheritDoc} */ diff --git a/src/de/inetsoftware/jwebassembly/module/WasmLocalInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmLocalInstruction.java index bc51561..b3446df 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmLocalInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmLocalInstruction.java @@ -21,6 +21,7 @@ import java.io.IOException; import javax.annotation.Nonnegative; import javax.annotation.Nonnull; +import de.inetsoftware.jwebassembly.module.WasmInstruction.Type; import de.inetsoftware.jwebassembly.wasm.ValueType; /** @@ -51,6 +52,14 @@ class WasmLocalInstruction extends WasmInstruction { this.idx = idx; } + /** + * {@inheritDoc} + */ + @Override + Type getType() { + return Type.Local; + } + /** * {@inheritDoc} */ diff --git a/src/de/inetsoftware/jwebassembly/module/WasmNopInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmNopInstruction.java index afeae00..78895a6 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmNopInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmNopInstruction.java @@ -20,6 +20,7 @@ import java.io.IOException; import javax.annotation.Nonnull; +import de.inetsoftware.jwebassembly.module.WasmInstruction.Type; import de.inetsoftware.jwebassembly.wasm.ValueType; /** @@ -40,6 +41,14 @@ class WasmNopInstruction extends WasmInstruction { super( javaCodePos ); } + /** + * {@inheritDoc} + */ + @Override + Type getType() { + return Type.Nop; + } + /** * {@inheritDoc} */ diff --git a/src/de/inetsoftware/jwebassembly/module/WasmNumericInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmNumericInstruction.java index 0e8aa2d..b3b0d0e 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmNumericInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmNumericInstruction.java @@ -21,6 +21,7 @@ import java.io.IOException; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import de.inetsoftware.jwebassembly.module.WasmInstruction.Type; import de.inetsoftware.jwebassembly.wasm.NumericOperator; import de.inetsoftware.jwebassembly.wasm.ValueType; @@ -52,6 +53,14 @@ class WasmNumericInstruction extends WasmInstruction { this.valueType = valueType; } + /** + * {@inheritDoc} + */ + @Override + Type getType() { + return Type.Numeric; + } + /** * {@inheritDoc} */ diff --git a/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java index c4fb938..8b5f93c 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java @@ -22,6 +22,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import de.inetsoftware.jwebassembly.WasmException; +import de.inetsoftware.jwebassembly.module.WasmInstruction.Type; import de.inetsoftware.jwebassembly.wasm.ArrayOperator; import de.inetsoftware.jwebassembly.wasm.StorageType; import de.inetsoftware.jwebassembly.wasm.StructOperator; @@ -55,6 +56,14 @@ class WasmStructInstruction extends WasmInstruction { this.type = type; } + /** + * {@inheritDoc} + */ + @Override + Type getType() { + return Type.Struct; + } + /** * {@inheritDoc} */