2018-12-05 23:30:46 +01:00
/ *
2019-01-13 11:36:07 +01:00
Copyright 2018 - 2019 Volker Berlin ( i - net software )
2018-12-05 23:30:46 +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 ;
2019-04-22 21:24:22 +02:00
import java.util.List ;
2018-12-05 23:30:46 +01:00
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 ;
2019-01-14 20:09:00 +01:00
import de.inetsoftware.jwebassembly.wasm.AnyType ;
2019-04-21 21:33:22 +02:00
import de.inetsoftware.jwebassembly.wasm.NamedStorageType ;
2018-12-05 23:30:46 +01:00
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 {
2019-04-21 21:33:22 +02:00
private final StructOperator op ;
2018-12-05 23:30:46 +01:00
2019-04-21 21:33:22 +02:00
private final StructType type ;
2018-12-05 23:30:46 +01:00
2019-04-21 21:33:22 +02:00
private final NamedStorageType fieldName ;
2019-01-13 11:36:07 +01:00
2018-12-05 23:30:46 +01:00
/ * *
* Create an instance of numeric operation .
*
* @param op
* the struct operation
2019-01-23 20:27:57 +01:00
* @param type
* the type of the parameters
2019-01-13 11:36:07 +01:00
* @param fieldName
* the name of field if needed for the operation
2018-12-05 23:30:46 +01:00
* @param javaCodePos
* the code position / offset in the Java method
2019-03-31 11:23:45 +02:00
* @param lineNumber
* the line number in the Java source code
2018-12-05 23:30:46 +01:00
* /
2019-04-21 21:33:22 +02:00
WasmStructInstruction ( @Nullable StructOperator op , @Nullable StructType type , @Nullable NamedStorageType fieldName , int javaCodePos , int lineNumber ) {
2019-03-31 11:23:45 +02:00
super ( javaCodePos , lineNumber ) ;
2018-12-05 23:30:46 +01:00
this . op = op ;
2019-01-23 20:27:57 +01:00
this . type = type ;
2019-01-13 11:36:07 +01:00
this . fieldName = fieldName ;
2018-12-05 23:30:46 +01:00
}
2019-04-20 21:41:46 +02:00
/ * *
* Get the StructOperator
*
* @return the operator
* /
StructOperator getOperator ( ) {
return op ;
}
2018-12-19 20:10:26 +01:00
/ * *
2019-01-23 20:27:57 +01:00
* Get the struct type of this instruction .
2018-12-19 20:10:26 +01:00
*
* @return the type
* /
2019-01-23 20:27:57 +01:00
StructType getStructType ( ) {
return 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 {
2019-04-22 21:24:22 +02:00
int idx = - 1 ;
if ( type ! = null & & fieldName ! = null ) {
// The fieldName of the struct operation does not contain the class name in which the field was declared. It contains the class name of the variable. This can be the class or a subclass.
List < NamedStorageType > fields = type . getFields ( ) ;
boolean classNameMatched = false ;
for ( int i = fields . size ( ) - 1 ; i > = 0 ; i - - ) {
NamedStorageType field = fields . get ( i ) ;
if ( ! classNameMatched & & field . geClassName ( ) . equals ( fieldName . geClassName ( ) ) ) {
classNameMatched = true ;
}
if ( classNameMatched & & field . getName ( ) . equals ( fieldName . getName ( ) ) ) {
idx = i ;
break ;
}
}
}
2019-04-22 15:56:11 +02:00
writer . writeStructOperator ( op , type , fieldName , idx ) ;
2018-12-05 23:30:46 +01:00
}
/ * *
* { @inheritDoc }
* /
2019-01-20 19:58:23 +01:00
AnyType getPushValueType ( ) {
2018-12-05 23:30:46 +01:00
switch ( op ) {
2019-01-20 11:31:33 +01:00
case NULL :
2018-12-05 23:30:46 +01:00
return ValueType . anyref ;
2019-01-23 20:27:57 +01:00
case NEW :
case NEW_DEFAULT :
2018-12-05 23:30:46 +01:00
case GET :
2019-01-20 19:58:23 +01:00
return type ;
2018-12-05 23:30:46 +01:00
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 :
2019-01-20 19:58:23 +01:00
case NULL :
2018-12-05 23:30:46 +01:00
return 0 ;
default :
throw new WasmException ( " Unknown array operation: " + op , - 1 ) ;
}
}
}