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 ); size = Math.max( size, slot + 1 );
LocaleVariable var = variables[slot]; LocaleVariable var = variables[slot];
if( var.valueType != null && var.valueType != valueType ) { if( var.valueType != null && var.valueType != valueType ) {
throw new WasmException( "Redefine local variable type from " + var.valueType + " to " if( var.valueType.getCode() >= 0 && valueType == ValueType.anyref ) {
+ valueType, null, null, -1 ); 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; var.valueType = valueType;
} }

View File

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

View File

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

View File

@ -41,6 +41,8 @@ public abstract class WasmCodeBuilder {
private final List<WasmInstruction> instructions = new ArrayList<>(); private final List<WasmInstruction> instructions = new ArrayList<>();
private TypeManager types;
/** /**
* Get the list of instructions * Get the list of instructions
* *
@ -50,6 +52,16 @@ public abstract class WasmCodeBuilder {
return instructions; 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. * 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 * the code position/offset in the Java method
*/ */
protected void addStructInstruction( StructOperator op, @Nullable String typeName, @Nullable String fieldName, int javaCodePos ) { 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 StructOperator op;
private final String typeName; private final StructType type;
private AnyType type;
private final String fieldName; private final String fieldName;
@ -48,37 +46,27 @@ class WasmStructInstruction extends WasmInstruction {
* *
* @param op * @param op
* the struct operation * the struct operation
* @param typeName * @param type
* the type name of the parameters * the type of the parameters
* @param fieldName * @param fieldName
* the name of field if needed for the operation * the name of field if needed for the operation
* @param javaCodePos * @param javaCodePos
* the code position/offset in the Java method * 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 ); super( javaCodePos );
this.op = op; this.op = op;
this.typeName = typeName; this.type = type;
this.fieldName = fieldName; this.fieldName = fieldName;
} }
/** /**
* Get the type name of this instruction. * Get the struct type of this instruction.
* *
* @return the type * @return the type
*/ */
String getTypeName() { StructType getStructType() {
return typeName; return type;
}
/**
* Set the resolved StructType for the typeName
*
* @param type
* the type
*/
void setType( StructType type ) {
this.type = type;
} }
/** /**
@ -101,10 +89,10 @@ class WasmStructInstruction extends WasmInstruction {
*/ */
AnyType getPushValueType() { AnyType getPushValueType() {
switch( op ) { switch( op ) {
case NEW:
case NEW_DEFAULT:
case NULL: case NULL:
return ValueType.anyref; return ValueType.anyref;
case NEW:
case NEW_DEFAULT:
case GET: case GET:
return type; return type;
case SET: case SET: