normalization the local instruction

This commit is contained in:
Volker Berlin 2019-01-27 21:13:48 +01:00
parent 80e922fa26
commit ff345e68c2
3 changed files with 19 additions and 38 deletions

View File

@ -35,7 +35,7 @@ abstract class WasmInstruction {
* Type of instruction to faster differ as with instanceof. * Type of instruction to faster differ as with instanceof.
*/ */
static enum Type { 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; private int javaCodePos;

View File

@ -16,12 +16,8 @@
*/ */
package de.inetsoftware.jwebassembly.module; package de.inetsoftware.jwebassembly.module;
import java.io.IOException;
import javax.annotation.Nonnegative; import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import de.inetsoftware.jwebassembly.module.WasmInstruction.Type;
import de.inetsoftware.jwebassembly.wasm.AnyType; import de.inetsoftware.jwebassembly.wasm.AnyType;
/** /**
@ -30,11 +26,7 @@ import de.inetsoftware.jwebassembly.wasm.AnyType;
* @author Volker Berlin * @author Volker Berlin
* *
*/ */
class WasmLoadStoreInstruction extends WasmInstruction { class WasmLoadStoreInstruction extends WasmLocalInstruction {
private boolean load;
private int idx;
private LocaleVariableManager localVariables; private LocaleVariableManager localVariables;
@ -51,9 +43,7 @@ class WasmLoadStoreInstruction extends WasmInstruction {
* the code position/offset in the Java method * the code position/offset in the Java method
*/ */
WasmLoadStoreInstruction( boolean load, @Nonnegative int idx, LocaleVariableManager localVariables, int javaCodePos ) { WasmLoadStoreInstruction( boolean load, @Nonnegative int idx, LocaleVariableManager localVariables, int javaCodePos ) {
super( javaCodePos ); super( load, idx, javaCodePos );
this.load = load;
this.idx = idx;
this.localVariables = localVariables; this.localVariables = localVariables;
} }
@ -61,34 +51,14 @@ class WasmLoadStoreInstruction extends WasmInstruction {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
Type getType() { int getIndex() {
return Type.LoadStore; return localVariables.get( super.getIndex() ); // translate slot index to position index
}
/**
* {@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 );
}
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
AnyType getPushValueType() { AnyType getPushValueType() {
return load ? localVariables.getValueType( idx ) : null; return getPopCount() == 0 ? localVariables.getValueType( super.getIndex() ) : null;
}
/**
* {@inheritDoc}
*/
@Override
int getPopCount() {
return load ? 0 : 1;
} }
} }

View File

@ -60,14 +60,25 @@ class WasmLocalInstruction extends WasmInstruction {
return Type.Local; return Type.Local;
} }
/**
* Get the number of the locals
*
* @return the index
*/
@Nonnegative
int getIndex() {
return idx;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
int index = getIndex();
if( load ) { if( load ) {
writer.writeLoad( idx ); writer.writeLoad( index );
} else { } else {
writer.writeStore( idx ); writer.writeStore( index );
} }
} }