Add method getPushValueType()

This commit is contained in:
Volker 2018-08-06 12:52:44 +02:00
parent d3e80bf709
commit cd8dd10182
9 changed files with 100 additions and 4 deletions

View File

@ -145,6 +145,17 @@ class LocaleVariableManager {
return variables[slot].idx; 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. * Ensure that there is enough capacity.
* *

View File

@ -55,4 +55,11 @@ class WasmBlockInstruction extends WasmInstruction {
public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
writer.writeBlockCode( op, data ); writer.writeBlockCode( op, data );
} }
/**
* {@inheritDoc}
*/
ValueType getPushValueType() {
return op == WasmBlockOperator.IF && data != ValueType.empty ? (ValueType)data : null;
}
} }

View File

@ -32,6 +32,8 @@ class WasmCallInstruction extends WasmInstruction {
private final String name; private final String name;
private final ValueType valueType;
/** /**
* Create an instance of a function call instruction * Create an instance of a function call instruction
* *
@ -43,6 +45,8 @@ class WasmCallInstruction extends WasmInstruction {
WasmCallInstruction( ConstantRef method, int javaCodePos ) { WasmCallInstruction( ConstantRef method, int javaCodePos ) {
super( javaCodePos ); super( javaCodePos );
this.name = new FunctionName( method ).signatureName; 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 { public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
writer.writeFunctionCall( name ); writer.writeFunctionCall( name );
} }
/**
* {@inheritDoc}
*/
ValueType getPushValueType() {
return valueType;
}
} }

View File

@ -92,4 +92,11 @@ class WasmConstInstruction extends WasmInstruction {
public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
writer.writeConst( value, valueType ); writer.writeConst( value, valueType );
} }
/**
* {@inheritDoc}
*/
ValueType getPushValueType() {
return valueType;
}
} }

View File

@ -47,4 +47,33 @@ class WasmConvertInstruction extends WasmInstruction {
public void writeTo( ModuleWriter writer ) throws IOException { public void writeTo( ModuleWriter writer ) throws IOException {
writer.writeCast( conversion ); 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() );
}
}
} }

View File

@ -19,6 +19,7 @@ package de.inetsoftware.jwebassembly.module;
import java.io.IOException; import java.io.IOException;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/** /**
* Base class of all WasmInstruction. * Base class of all WasmInstruction.
@ -65,4 +66,12 @@ abstract class WasmInstruction {
void setCodePosition( int newPos ) { void setCodePosition( int newPos ) {
this.javaCodePos = 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();
} }

View File

@ -58,11 +58,18 @@ class WasmLoadStoreInstruction extends WasmInstruction {
* {@inheritDoc} * {@inheritDoc}
*/ */
public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { 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 ) { if( load ) {
writer.writeLoad( idx ); writer.writeLoad( index );
} else { } else {
writer.writeStore( idx ); writer.writeStore( index );
} }
} }
/**
* {@inheritDoc}
*/
ValueType getPushValueType() {
return load ? localVariables.getValueType( idx ) : null;
}
} }

View File

@ -44,4 +44,11 @@ class WasmNopInstruction extends WasmInstruction {
public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
// nothing // nothing
} }
/**
* {@inheritDoc}
*/
ValueType getPushValueType() {
return null;
}
} }

View File

@ -55,4 +55,11 @@ class WasmNumericInstruction extends WasmInstruction {
public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
writer.writeNumericOperator( numOp, valueType ); writer.writeNumericOperator( numOp, valueType );
} }
/**
* {@inheritDoc}
*/
ValueType getPushValueType() {
return valueType;
}
} }