From ff345e68c22c2a2b9c5512f287ff55b192839b9f Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Sun, 27 Jan 2019 21:13:48 +0100 Subject: [PATCH] normalization the local instruction --- .../jwebassembly/module/WasmInstruction.java | 2 +- .../module/WasmLoadStoreInstruction.java | 40 +++---------------- .../module/WasmLocalInstruction.java | 15 ++++++- 3 files changed, 19 insertions(+), 38 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java index 302ee48..48bd8fd 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java @@ -35,7 +35,7 @@ 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; + Const, Convert, Local, Global, Block, Numeric, Nop, Call, Array, Struct; } private int javaCodePos; diff --git a/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java index e33562f..12c62ca 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java @@ -16,12 +16,8 @@ */ package de.inetsoftware.jwebassembly.module; -import java.io.IOException; - import javax.annotation.Nonnegative; -import javax.annotation.Nonnull; -import de.inetsoftware.jwebassembly.module.WasmInstruction.Type; import de.inetsoftware.jwebassembly.wasm.AnyType; /** @@ -30,11 +26,7 @@ import de.inetsoftware.jwebassembly.wasm.AnyType; * @author Volker Berlin * */ -class WasmLoadStoreInstruction extends WasmInstruction { - - private boolean load; - - private int idx; +class WasmLoadStoreInstruction extends WasmLocalInstruction { private LocaleVariableManager localVariables; @@ -51,9 +43,7 @@ class WasmLoadStoreInstruction extends WasmInstruction { * the code position/offset in the Java method */ WasmLoadStoreInstruction( boolean load, @Nonnegative int idx, LocaleVariableManager localVariables, int javaCodePos ) { - super( javaCodePos ); - this.load = load; - this.idx = idx; + super( load, idx, javaCodePos ); this.localVariables = localVariables; } @@ -61,34 +51,14 @@ class WasmLoadStoreInstruction extends WasmInstruction { * {@inheritDoc} */ @Override - Type getType() { - return Type.LoadStore; - } - - /** - * {@inheritDoc} - */ - public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { - int index = localVariables.get( idx ); // translate slot index to position index - if( load ) { - writer.writeLoad( index ); - } else { - writer.writeStore( index ); - } + int getIndex() { + return localVariables.get( super.getIndex() ); // translate slot index to position index } /** * {@inheritDoc} */ AnyType getPushValueType() { - return load ? localVariables.getValueType( idx ) : null; - } - - /** - * {@inheritDoc} - */ - @Override - int getPopCount() { - return load ? 0 : 1; + return getPopCount() == 0 ? localVariables.getValueType( super.getIndex() ) : null; } } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmLocalInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmLocalInstruction.java index 658df2f..9e8eb39 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmLocalInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmLocalInstruction.java @@ -60,14 +60,25 @@ class WasmLocalInstruction extends WasmInstruction { return Type.Local; } + /** + * Get the number of the locals + * + * @return the index + */ + @Nonnegative + int getIndex() { + return idx; + } + /** * {@inheritDoc} */ public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { + int index = getIndex(); if( load ) { - writer.writeLoad( idx ); + writer.writeLoad( index ); } else { - writer.writeStore( idx ); + writer.writeStore( index ); } }