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.
*/
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;

View File

@ -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;
}
}

View File

@ -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 );
}
}