2018-12-05 23:30:46 +01:00
|
|
|
/*
|
|
|
|
Copyright 2018 Volker Berlin (i-net software)
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
|
|
|
|
*/
|
|
|
|
package de.inetsoftware.jwebassembly.module;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
|
|
|
import de.inetsoftware.jwebassembly.WasmException;
|
2019-01-06 16:29:26 +01:00
|
|
|
import de.inetsoftware.jwebassembly.module.TypeManager.StructType;
|
2018-12-05 23:30:46 +01:00
|
|
|
import de.inetsoftware.jwebassembly.wasm.StorageType;
|
|
|
|
import de.inetsoftware.jwebassembly.wasm.StructOperator;
|
|
|
|
import de.inetsoftware.jwebassembly.wasm.ValueType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WasmInstruction for struct operation. A struct is like a Java class without methods.
|
|
|
|
*
|
|
|
|
* @author Volker Berlin
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class WasmStructInstruction extends WasmInstruction {
|
|
|
|
|
|
|
|
private final StructOperator op;
|
|
|
|
|
2019-01-06 16:29:26 +01:00
|
|
|
private final String typeName;
|
|
|
|
|
|
|
|
private StorageType type;
|
2018-12-05 23:30:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an instance of numeric operation.
|
|
|
|
*
|
|
|
|
* @param op
|
|
|
|
* the struct operation
|
2019-01-06 16:29:26 +01:00
|
|
|
* @param typeName
|
|
|
|
* the type name of the parameters
|
2018-12-05 23:30:46 +01:00
|
|
|
* @param javaCodePos
|
|
|
|
* the code position/offset in the Java method
|
|
|
|
*/
|
2019-01-06 16:29:26 +01:00
|
|
|
WasmStructInstruction( @Nullable StructOperator op, @Nullable String typeName, int javaCodePos ) {
|
2018-12-05 23:30:46 +01:00
|
|
|
super( javaCodePos );
|
|
|
|
this.op = op;
|
2019-01-06 16:29:26 +01:00
|
|
|
this.typeName = typeName;
|
2018-12-05 23:30:46 +01:00
|
|
|
}
|
|
|
|
|
2018-12-19 20:10:26 +01:00
|
|
|
/**
|
2019-01-06 16:29:26 +01:00
|
|
|
* Get the type name of this instruction.
|
2018-12-19 20:10:26 +01:00
|
|
|
*
|
|
|
|
* @return the type
|
|
|
|
*/
|
2019-01-06 16:29:26 +01:00
|
|
|
String getTypeName() {
|
|
|
|
return typeName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the resolved StructType for the typeName
|
|
|
|
*
|
|
|
|
* @param type
|
|
|
|
* the type
|
|
|
|
*/
|
|
|
|
void setType( StructType type ) {
|
|
|
|
this.type = type;
|
2018-12-19 20:10:26 +01:00
|
|
|
}
|
|
|
|
|
2018-12-16 18:22:44 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
Type getType() {
|
|
|
|
return Type.Struct;
|
|
|
|
}
|
|
|
|
|
2018-12-05 23:30:46 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
|
|
|
|
writer.writeStructOperator( op, type );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
ValueType getPushValueType() {
|
|
|
|
switch( op ) {
|
|
|
|
case NEW:
|
|
|
|
case NEW_DEFAULT:
|
|
|
|
return ValueType.anyref;
|
|
|
|
case GET:
|
|
|
|
return type instanceof ValueType ? (ValueType)type : ValueType.anyref;
|
|
|
|
case SET:
|
|
|
|
return null;
|
|
|
|
default:
|
|
|
|
throw new WasmException( "Unknown array operation: " + op, -1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
int getPopCount() {
|
|
|
|
switch( op ) {
|
|
|
|
case NEW:
|
|
|
|
case NEW_DEFAULT:
|
|
|
|
case GET:
|
|
|
|
return 1;
|
|
|
|
case SET:
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
throw new WasmException( "Unknown array operation: " + op, -1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|