JWebAssembly/src/de/inetsoftware/jwebassembly/module/WasmArrayInstruction.java

108 lines
2.7 KiB
Java
Raw Normal View History

2018-12-02 19:54:59 +01:00
/*
2019-01-14 20:09:00 +01:00
Copyright 2018 - 2019 Volker Berlin (i-net software)
2018-12-02 19:54:59 +01:00
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;
2018-12-03 21:09:22 +01:00
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
2019-01-14 20:09:00 +01:00
import de.inetsoftware.jwebassembly.wasm.AnyType;
2018-12-03 21:09:22 +01:00
import de.inetsoftware.jwebassembly.wasm.ValueType;
2018-12-02 19:54:59 +01:00
/**
2018-12-05 17:55:15 +01:00
* WasmInstruction for an array operation.
2018-12-02 19:54:59 +01:00
*
* @author Volker Berlin
*
*/
class WasmArrayInstruction extends WasmInstruction {
private final ArrayOperator op;
2019-01-14 20:09:00 +01:00
private final AnyType type;
2018-12-02 19:54:59 +01:00
/**
2018-12-05 17:55:15 +01:00
* Create an instance of an array operation.
2018-12-02 19:54:59 +01:00
*
* @param op
* the array operation
* @param type
2018-12-02 19:54:59 +01:00
* the type of the parameters
* @param javaCodePos
* the code position/offset in the Java method
*/
2019-01-14 20:09:00 +01:00
WasmArrayInstruction( @Nullable ArrayOperator op, @Nullable AnyType type, int javaCodePos ) {
2018-12-02 19:54:59 +01:00
super( javaCodePos );
this.op = op;
this.type = type;
2018-12-02 19:54:59 +01:00
}
/**
2018-12-16 18:22:44 +01:00
* {@inheritDoc}
*/
@Override
Type getType() {
return Type.Array;
}
/**
2018-12-02 19:54:59 +01:00
* {@inheritDoc}
*/
public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
writer.writeArrayOperator( op, type );
2018-12-02 19:54:59 +01:00
}
/**
* {@inheritDoc}
*/
AnyType getPushValueType() {
2018-12-02 19:54:59 +01:00
switch( op ) {
case NEW:
return ValueType.anyref;
case GET:
return type instanceof ValueType ? (ValueType)type : ValueType.anyref;
2018-12-02 19:54:59 +01:00
case SET:
return null;
case LENGTH:
return ValueType.i32;
default:
throw new WasmException( "Unknown array operation: " + op, -1 );
}
}
/**
* {@inheritDoc}
*/
@Override
int getPopCount() {
switch( op ) {
case NEW:
case GET:
return 2;
2018-12-02 19:54:59 +01:00
case LENGTH:
return 1;
case SET:
return 3;
2018-12-02 19:54:59 +01:00
default:
throw new WasmException( "Unknown array operation: " + op, -1 );
}
}
}