From cd8dd10182e383cc8bf0042db4623e56bfe5b3f4 Mon Sep 17 00:00:00 2001 From: Volker Date: Mon, 6 Aug 2018 12:52:44 +0200 Subject: [PATCH] Add method getPushValueType() --- .../module/LocaleVariableManager.java | 11 +++++++ .../module/WasmBlockInstruction.java | 7 +++++ .../module/WasmCallInstruction.java | 14 ++++++++- .../module/WasmConstInstruction.java | 7 +++++ .../module/WasmConvertInstruction.java | 29 +++++++++++++++++++ .../jwebassembly/module/WasmInstruction.java | 9 ++++++ .../module/WasmLoadStoreInstruction.java | 13 +++++++-- .../module/WasmNopInstruction.java | 7 +++++ .../module/WasmNumericInstruction.java | 7 +++++ 9 files changed, 100 insertions(+), 4 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java b/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java index 86bf3e4..7c0abd2 100644 --- a/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java +++ b/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java @@ -145,6 +145,17 @@ class LocaleVariableManager { return variables[slot].idx; } + /** + * Get the ValueType of the variable. + * + * @param slot + * the memory/slot index of the local variable in Java + * @return the ValueType + */ + ValueType getValueType( int slot ) { + return variables[slot].valueType; + } + /** * Ensure that there is enough capacity. * diff --git a/src/de/inetsoftware/jwebassembly/module/WasmBlockInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmBlockInstruction.java index b98f053..38957a6 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmBlockInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmBlockInstruction.java @@ -55,4 +55,11 @@ class WasmBlockInstruction extends WasmInstruction { public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { writer.writeBlockCode( op, data ); } + + /** + * {@inheritDoc} + */ + ValueType getPushValueType() { + return op == WasmBlockOperator.IF && data != ValueType.empty ? (ValueType)data : null; + } } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java index d5aa552..82bc7cf 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java @@ -30,7 +30,9 @@ import de.inetsoftware.classparser.ConstantRef; */ class WasmCallInstruction extends WasmInstruction { - private final String name; + private final String name; + + private final ValueType valueType; /** * Create an instance of a function call instruction @@ -43,6 +45,8 @@ class WasmCallInstruction extends WasmInstruction { WasmCallInstruction( ConstantRef method, int javaCodePos ) { super( javaCodePos ); this.name = new FunctionName( method ).signatureName; + String signature = method.getType(); + this.valueType = ModuleGenerator.getValueType( signature, signature.indexOf( ')' ) + 1 ); } /** @@ -51,4 +55,12 @@ class WasmCallInstruction extends WasmInstruction { public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { writer.writeFunctionCall( name ); } + + + /** + * {@inheritDoc} + */ + ValueType getPushValueType() { + return valueType; + } } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java index 668309f..e7002a7 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java @@ -92,4 +92,11 @@ class WasmConstInstruction extends WasmInstruction { public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { writer.writeConst( value, valueType ); } + + /** + * {@inheritDoc} + */ + ValueType getPushValueType() { + return valueType; + } } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmConvertInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmConvertInstruction.java index a826850..947e1fd 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmConvertInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmConvertInstruction.java @@ -47,4 +47,33 @@ class WasmConvertInstruction extends WasmInstruction { public void writeTo( ModuleWriter writer ) throws IOException { writer.writeCast( conversion ); } + + /** + * {@inheritDoc} + */ + ValueType getPushValueType() { + switch( conversion ) { + case l2i: + case f2i: + case d2i: + case i2b: + case i2c: + case i2s: + return ValueType.i32; + case i2l: + case f2l: + case d2l: + return ValueType.i64; + case i2f: + case l2f: + case d2f: + return ValueType.f32; + case i2d: + case l2d: + case f2d: + return ValueType.f64; + default: + throw new Error( conversion.toString() ); + } + } } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java index a69473a..2cf9d49 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java @@ -19,6 +19,7 @@ package de.inetsoftware.jwebassembly.module; import java.io.IOException; import javax.annotation.Nonnull; +import javax.annotation.Nullable; /** * Base class of all WasmInstruction. @@ -65,4 +66,12 @@ abstract class WasmInstruction { void setCodePosition( int newPos ) { this.javaCodePos = newPos; } + + /** + * Get the ValueType if this instruction push a value on the stack. + * + * @return the ValueType or null if no value is push + */ + @Nullable + abstract ValueType getPushValueType(); } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java index f4060cb..dffc617 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java @@ -58,11 +58,18 @@ class WasmLoadStoreInstruction extends WasmInstruction { * {@inheritDoc} */ public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { - idx = localVariables.get( idx ); // translate slot index to position index + int index = localVariables.get( idx ); // translate slot index to position index if( load ) { - writer.writeLoad( idx ); + writer.writeLoad( index ); } else { - writer.writeStore( idx ); + writer.writeStore( index ); } } + + /** + * {@inheritDoc} + */ + ValueType getPushValueType() { + return load ? localVariables.getValueType( idx ) : null; + } } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmNopInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmNopInstruction.java index e639355..fb21aac 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmNopInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmNopInstruction.java @@ -44,4 +44,11 @@ class WasmNopInstruction extends WasmInstruction { public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { // nothing } + + /** + * {@inheritDoc} + */ + ValueType getPushValueType() { + return null; + } } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmNumericInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmNumericInstruction.java index 1455bea..e644261 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmNumericInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmNumericInstruction.java @@ -55,4 +55,11 @@ class WasmNumericInstruction extends WasmInstruction { public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { writer.writeNumericOperator( numOp, valueType ); } + + /** + * {@inheritDoc} + */ + ValueType getPushValueType() { + return valueType; + } }