Pass the StructType of WasmStructInstruction to the LocalevariableManager

This commit is contained in:
Volker Berlin 2019-01-23 20:27:57 +01:00
parent 24f1c24a84
commit 0d0e606983
5 changed files with 39 additions and 36 deletions

View File

@ -83,8 +83,14 @@ class LocaleVariableManager {
size = Math.max( size, slot + 1 );
LocaleVariable var = variables[slot];
if( var.valueType != null && var.valueType != valueType ) {
throw new WasmException( "Redefine local variable type from " + var.valueType + " to "
+ valueType, null, null, -1 );
if( var.valueType.getCode() >= 0 && valueType == ValueType.anyref ) {
return;
}
if( valueType.getCode() >= 0 && var.valueType == ValueType.anyref ) {
// set the more specific type
} else {
throw new WasmException( "Redefine local variable type from " + var.valueType + " to " + valueType, null, null, -1 );
}
}
var.valueType = valueType;
}

View File

@ -77,6 +77,8 @@ public class ModuleGenerator {
public ModuleGenerator( @Nonnull ModuleWriter writer, List<URL> libraries ) {
this.writer = writer;
this.libraries = new URLClassLoader( libraries.toArray( new URL[libraries.size()] ) );
javaCodeBuilder.init( types );
((WasmCodeBuilder)watParser).init( types );
}
/**
@ -155,13 +157,9 @@ public class ModuleGenerator {
* if any I/O error occur
*/
private void setStructType( WasmStructInstruction instruction ) throws IOException {
String name = instruction.getTypeName();
if( name != null ) {
StructType type = types.valueOf( name );
instruction.setType( type );
if( type.getCode() == Integer.MAX_VALUE ) {
writeStructType( type );
}
StructType type = instruction.getStructType();
if( type != null && type.getCode() == Integer.MAX_VALUE ) {
writeStructType( type );
}
}

View File

@ -22,7 +22,6 @@ import java.util.Iterator;
import javax.annotation.Nonnull;
import de.inetsoftware.jwebassembly.wasm.AnyType;
import de.inetsoftware.jwebassembly.wasm.ValueType;
/**
* WasmInstruction for a function call.
@ -32,7 +31,7 @@ import de.inetsoftware.jwebassembly.wasm.ValueType;
*/
class WasmCallInstruction extends WasmInstruction {
private ValueType valueType;
private AnyType valueType;
private final FunctionName name;
@ -105,9 +104,9 @@ class WasmCallInstruction extends WasmInstruction {
while( parser.next() != null ) {
paramCount++;
}
valueType = (ValueType)parser.next();
valueType = parser.next();
while( parser.hasNext() ) {
valueType = (ValueType)parser.next();
valueType = parser.next();
paramCount--;
}
}

View File

@ -41,6 +41,8 @@ public abstract class WasmCodeBuilder {
private final List<WasmInstruction> instructions = new ArrayList<>();
private TypeManager types;
/**
* Get the list of instructions
*
@ -50,6 +52,16 @@ public abstract class WasmCodeBuilder {
return instructions;
}
/**
* Initialize the code builder;
*
* @param types
* the type manager
*/
void init( TypeManager types ) {
this.types = types;
}
/**
* Get the data types of the local variables. The value is only valid until the next call.
*
@ -239,6 +251,6 @@ public abstract class WasmCodeBuilder {
* the code position/offset in the Java method
*/
protected void addStructInstruction( StructOperator op, @Nullable String typeName, @Nullable String fieldName, int javaCodePos ) {
instructions.add( new WasmStructInstruction( op, typeName, fieldName, javaCodePos ) );
instructions.add( new WasmStructInstruction( op, typeName == null ? null : types.valueOf( typeName ), fieldName, javaCodePos ) );
}
}

View File

@ -37,9 +37,7 @@ class WasmStructInstruction extends WasmInstruction {
private final StructOperator op;
private final String typeName;
private AnyType type;
private final StructType type;
private final String fieldName;
@ -48,37 +46,27 @@ class WasmStructInstruction extends WasmInstruction {
*
* @param op
* the struct operation
* @param typeName
* the type name of the parameters
* @param type
* the type of the parameters
* @param fieldName
* the name of field if needed for the operation
* @param javaCodePos
* the code position/offset in the Java method
*/
WasmStructInstruction( @Nullable StructOperator op, @Nullable String typeName, @Nullable String fieldName, int javaCodePos ) {
WasmStructInstruction( @Nullable StructOperator op, @Nullable StructType type, @Nullable String fieldName, int javaCodePos ) {
super( javaCodePos );
this.op = op;
this.typeName = typeName;
this.type = type;
this.fieldName = fieldName;
}
/**
* Get the type name of this instruction.
* Get the struct type of this instruction.
*
* @return the type
*/
String getTypeName() {
return typeName;
}
/**
* Set the resolved StructType for the typeName
*
* @param type
* the type
*/
void setType( StructType type ) {
this.type = type;
StructType getStructType() {
return type;
}
/**
@ -101,10 +89,10 @@ class WasmStructInstruction extends WasmInstruction {
*/
AnyType getPushValueType() {
switch( op ) {
case NEW:
case NEW_DEFAULT:
case NULL:
return ValueType.anyref;
case NEW:
case NEW_DEFAULT:
case GET:
return type;
case SET: