From b78e6054c6df57b7fb474a2dd355b134ffc2cadb Mon Sep 17 00:00:00 2001 From: Volker Date: Fri, 10 Aug 2018 18:25:38 +0200 Subject: [PATCH] Add getPopCount() --- .../module/WasmBlockInstruction.java | 8 ++++++ .../module/WasmCallInstruction.java | 24 +++++++++++++++--- .../module/WasmConstInstruction.java | 8 ++++++ .../module/WasmConvertInstruction.java | 8 ++++++ .../jwebassembly/module/WasmInstruction.java | 7 ++++++ .../module/WasmLoadStoreInstruction.java | 8 ++++++ .../module/WasmNopInstruction.java | 8 ++++++ .../module/WasmNumericInstruction.java | 25 ++++++++++++++++++- 8 files changed, 92 insertions(+), 4 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/module/WasmBlockInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmBlockInstruction.java index 38957a6..aafb3b0 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmBlockInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmBlockInstruction.java @@ -62,4 +62,12 @@ class WasmBlockInstruction extends WasmInstruction { ValueType getPushValueType() { return op == WasmBlockOperator.IF && data != ValueType.empty ? (ValueType)data : null; } + + /** + * {@inheritDoc} + */ + @Override + int getPopCount() { + return 0; + } } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java index 82bc7cf..11c81e0 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java @@ -30,7 +30,7 @@ import de.inetsoftware.classparser.ConstantRef; */ class WasmCallInstruction extends WasmInstruction { - private final String name; + private final ConstantRef method; private final ValueType valueType; @@ -44,7 +44,7 @@ class WasmCallInstruction extends WasmInstruction { */ WasmCallInstruction( ConstantRef method, int javaCodePos ) { super( javaCodePos ); - this.name = new FunctionName( method ).signatureName; + this.method = method; String signature = method.getType(); this.valueType = ModuleGenerator.getValueType( signature, signature.indexOf( ')' ) + 1 ); } @@ -53,7 +53,7 @@ class WasmCallInstruction extends WasmInstruction { * {@inheritDoc} */ public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { - writer.writeFunctionCall( name ); + writer.writeFunctionCall( new FunctionName( method ).signatureName ); } @@ -63,4 +63,22 @@ class WasmCallInstruction extends WasmInstruction { ValueType getPushValueType() { return valueType; } + + /** + * {@inheritDoc} + */ + @Override + int getPopCount() { + String signature = method.getType(); + int paramCount = 0; + ValueType type = null; + for( int i = 1; i < signature.length(); i++ ) { + if( signature.charAt( i ) == ')' ) { + return paramCount; + } + paramCount++; + ModuleGenerator.getValueType( signature, i ); + } + throw new Error(); + } } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java index e7002a7..4380b47 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java @@ -99,4 +99,12 @@ class WasmConstInstruction extends WasmInstruction { ValueType getPushValueType() { return valueType; } + + /** + * {@inheritDoc} + */ + @Override + int getPopCount() { + return 0; + } } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmConvertInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmConvertInstruction.java index 947e1fd..7e2e290 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmConvertInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmConvertInstruction.java @@ -76,4 +76,12 @@ class WasmConvertInstruction extends WasmInstruction { throw new Error( conversion.toString() ); } } + + /** + * {@inheritDoc} + */ + @Override + int getPopCount() { + return 1; + } } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java index 2cf9d49..da8d866 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java @@ -74,4 +74,11 @@ abstract class WasmInstruction { */ @Nullable abstract ValueType getPushValueType(); + + /** + * Get the count of values that are removed from the stack. + * + * @return the count + */ + abstract int getPopCount(); } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java index dffc617..eca5f5c 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java @@ -72,4 +72,12 @@ class WasmLoadStoreInstruction extends WasmInstruction { ValueType getPushValueType() { return load ? localVariables.getValueType( idx ) : null; } + + /** + * {@inheritDoc} + */ + @Override + int getPopCount() { + return load ? 0 : 1; + } } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmNopInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmNopInstruction.java index fb21aac..9f41756 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmNopInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmNopInstruction.java @@ -51,4 +51,12 @@ class WasmNopInstruction extends WasmInstruction { ValueType getPushValueType() { return null; } + + /** + * {@inheritDoc} + */ + @Override + int getPopCount() { + return 0; + } } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmNumericInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmNumericInstruction.java index e644261..f60ddf8 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmNumericInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmNumericInstruction.java @@ -60,6 +60,29 @@ class WasmNumericInstruction extends WasmInstruction { * {@inheritDoc} */ ValueType getPushValueType() { - return valueType; + switch( numOp ) { + case eq: + case ne: + case gt: + case lt_s: + case le_s: + case ge_s: + return null; + default: + return valueType; + } + } + + /** + * {@inheritDoc} + */ + @Override + int getPopCount() { + switch( numOp ) { + case neg: + return 1; + default: + return 2; + } } }