2018-12-02 19:54:59 +01:00
|
|
|
/*
|
2020-06-21 13:48:06 +02:00
|
|
|
Copyright 2018 - 2020 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;
|
2020-06-21 13:48:06 +02:00
|
|
|
import de.inetsoftware.jwebassembly.javascript.JavaScriptSyntheticFunctionName;
|
2019-01-14 20:09:00 +01:00
|
|
|
import de.inetsoftware.jwebassembly.wasm.AnyType;
|
2020-06-21 13:48:06 +02:00
|
|
|
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
|
|
|
|
import de.inetsoftware.jwebassembly.wasm.ArrayType;
|
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
|
|
|
|
2019-08-27 20:41:00 +02:00
|
|
|
private final TypeManager types;
|
|
|
|
|
2020-06-21 13:48:06 +02:00
|
|
|
private SyntheticFunctionName functionName;
|
|
|
|
|
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
|
|
|
*
|
2018-12-04 21:06:41 +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-03-31 11:23:45 +02:00
|
|
|
* @param lineNumber
|
|
|
|
* the line number in the Java source code
|
2018-12-02 19:54:59 +01:00
|
|
|
*/
|
2019-08-27 20:41:00 +02:00
|
|
|
WasmArrayInstruction( @Nullable ArrayOperator op, @Nullable AnyType type, TypeManager types, int javaCodePos, int lineNumber ) {
|
2019-03-31 11:23:45 +02:00
|
|
|
super( javaCodePos, lineNumber );
|
2018-12-02 19:54:59 +01:00
|
|
|
this.op = op;
|
2018-12-04 21:06:41 +01:00
|
|
|
this.type = type;
|
2019-08-27 20:41:00 +02:00
|
|
|
this.types = types;
|
2018-12-02 19:54:59 +01:00
|
|
|
}
|
|
|
|
|
2020-06-21 13:48:06 +02:00
|
|
|
/**
|
|
|
|
* Create the synthetic polyfill function of this instruction for nonGC mode.
|
|
|
|
*
|
|
|
|
* @return the function or null if not needed
|
|
|
|
*/
|
|
|
|
SyntheticFunctionName createNonGcFunction() {
|
|
|
|
// i8 and i16 are not valid in function signatures
|
|
|
|
AnyType functionType = type == ValueType.i8 || type == ValueType.i16 ? ValueType.i32 : type;
|
|
|
|
switch( op ) {
|
|
|
|
case NEW:
|
|
|
|
String cmd;
|
|
|
|
if( type.isRefType() ) {
|
|
|
|
cmd = "Object.seal(new Array(l).fill(null))";
|
|
|
|
} else {
|
|
|
|
switch( (ValueType)type ) {
|
|
|
|
case i8:
|
|
|
|
cmd = "new Uint8Array(l)";
|
|
|
|
break;
|
|
|
|
case i16:
|
|
|
|
cmd = "new Int16Array(l)";
|
|
|
|
break;
|
|
|
|
case i32:
|
|
|
|
cmd = "new Int32Array(l)";
|
|
|
|
break;
|
|
|
|
case i64:
|
|
|
|
cmd = "new BigInt64Array(l)";
|
|
|
|
break;
|
|
|
|
case f32:
|
|
|
|
cmd = "new Float32Array(l)";
|
|
|
|
break;
|
|
|
|
case f64:
|
|
|
|
cmd = "new Float64Array(l)";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cmd = "Object.seal(new Array(l).fill(null))";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ArrayType arrayType = types.arrayType( type );
|
2020-08-07 22:02:51 +02:00
|
|
|
functionName = new JavaScriptSyntheticFunctionName( "NonGC", "array_new_" + validJsName( type ), () -> {
|
2020-06-21 13:48:06 +02:00
|
|
|
// create the default values of a new type
|
2020-07-02 21:19:28 +02:00
|
|
|
return new StringBuilder( "(l)=>Object.seal({0:" ) // fix count of elements
|
2020-07-05 22:27:31 +02:00
|
|
|
.append( arrayType.getVTable() ) // .vtable
|
2020-06-21 13:48:06 +02:00
|
|
|
.append( ",1:0,2:" ) // .hashCode
|
|
|
|
.append( cmd ) // the array data
|
|
|
|
.append( "})" ) //
|
|
|
|
.toString();
|
2020-07-02 21:19:28 +02:00
|
|
|
}, ValueType.i32, null, ValueType.externref );
|
2020-06-21 13:48:06 +02:00
|
|
|
break;
|
|
|
|
case GET:
|
2020-08-07 22:02:51 +02:00
|
|
|
functionName = new JavaScriptSyntheticFunctionName( "NonGC", "array_get_" + validJsName( functionType ), () -> "(a,i)=>a[2][i]", ValueType.externref, ValueType.i32, null, functionType );
|
2020-06-21 13:48:06 +02:00
|
|
|
break;
|
|
|
|
case SET:
|
2020-08-07 22:02:51 +02:00
|
|
|
functionName = new JavaScriptSyntheticFunctionName( "NonGC", "array_set_" + validJsName( functionType ), () -> "(a,i,v)=>a[2][i]=v", ValueType.externref, ValueType.i32, functionType, null, null );
|
2020-06-21 13:48:06 +02:00
|
|
|
break;
|
|
|
|
case LEN:
|
2020-08-07 22:02:51 +02:00
|
|
|
functionName = new JavaScriptSyntheticFunctionName( "NonGC", "array_len", () -> "(a)=>a[2].length", ValueType.externref, null, ValueType.i32 );
|
2020-06-21 13:48:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return functionName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a valid JavaScript name.
|
|
|
|
*
|
|
|
|
* @param type
|
|
|
|
* the type
|
|
|
|
* @return the identifier that is valid
|
|
|
|
*/
|
|
|
|
private static String validJsName( AnyType type ) {
|
|
|
|
return type.isRefType() ? "obj" : type.toString();
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-07-02 21:19:28 +02:00
|
|
|
if( functionName != null ) { // nonGC
|
|
|
|
writer.writeFunctionCall( functionName, null );
|
|
|
|
} else {
|
|
|
|
writer.writeArrayOperator( op, type );
|
|
|
|
}
|
2018-12-02 19:54:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2019-01-20 19:58:23 +01:00
|
|
|
AnyType getPushValueType() {
|
2018-12-02 19:54:59 +01:00
|
|
|
switch( op ) {
|
|
|
|
case NEW:
|
2019-08-27 20:41:00 +02:00
|
|
|
return types.arrayType( type );
|
2018-12-02 19:54:59 +01:00
|
|
|
case GET:
|
2020-05-30 23:06:29 +02:00
|
|
|
return type instanceof ValueType ? (ValueType)type : ValueType.externref;
|
2018-12-02 19:54:59 +01:00
|
|
|
case SET:
|
|
|
|
return null;
|
2019-08-11 13:06:31 +02:00
|
|
|
case LEN:
|
2018-12-02 19:54:59 +01:00
|
|
|
return ValueType.i32;
|
|
|
|
default:
|
|
|
|
throw new WasmException( "Unknown array operation: " + op, -1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
int getPopCount() {
|
|
|
|
switch( op ) {
|
|
|
|
case GET:
|
2019-01-20 19:58:23 +01:00
|
|
|
return 2;
|
2020-05-30 23:06:29 +02:00
|
|
|
case NEW:
|
2019-08-11 13:06:31 +02:00
|
|
|
case LEN:
|
2018-12-02 19:54:59 +01:00
|
|
|
return 1;
|
|
|
|
case SET:
|
2019-01-20 19:58:23 +01:00
|
|
|
return 3;
|
2018-12-02 19:54:59 +01:00
|
|
|
default:
|
|
|
|
throw new WasmException( "Unknown array operation: " + op, -1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|