2019-05-05 17:25:43 +02:00
|
|
|
/*
|
|
|
|
Copyright 2019 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;
|
|
|
|
|
2019-09-14 15:22:25 +02:00
|
|
|
import de.inetsoftware.jwebassembly.javascript.JavaScriptSyntheticFunctionName;
|
2019-05-20 21:28:46 +02:00
|
|
|
import de.inetsoftware.jwebassembly.module.TypeManager.StructType;
|
2019-11-18 20:08:18 +01:00
|
|
|
import de.inetsoftware.jwebassembly.wasm.MemoryOperator;
|
2019-09-13 20:04:03 +02:00
|
|
|
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
|
|
|
|
import de.inetsoftware.jwebassembly.wasm.StructOperator;
|
2019-09-14 15:22:25 +02:00
|
|
|
import de.inetsoftware.jwebassembly.wasm.ValueType;
|
2019-09-13 20:04:03 +02:00
|
|
|
import de.inetsoftware.jwebassembly.wasm.VariableOperator;
|
2019-09-14 15:22:25 +02:00
|
|
|
import de.inetsoftware.jwebassembly.wasm.WasmOptions;
|
2019-05-20 21:28:46 +02:00
|
|
|
|
2019-05-05 17:25:43 +02:00
|
|
|
/**
|
|
|
|
* WasmInstruction for a function call.
|
|
|
|
*
|
|
|
|
* @author Volker Berlin
|
|
|
|
*
|
|
|
|
*/
|
2019-05-14 21:47:49 +02:00
|
|
|
class WasmCallIndirectInstruction extends WasmCallInstruction {
|
2019-05-05 17:25:43 +02:00
|
|
|
|
2019-06-09 21:09:05 +02:00
|
|
|
private int virtualFunctionIdx = -1;
|
2019-05-18 21:37:19 +02:00
|
|
|
|
2019-06-09 21:09:05 +02:00
|
|
|
private final StructType type;
|
|
|
|
|
2019-10-05 16:15:18 +02:00
|
|
|
private int tempVarIdx;
|
2019-05-20 21:28:46 +02:00
|
|
|
|
2019-09-14 15:22:25 +02:00
|
|
|
private final WasmOptions options;
|
|
|
|
|
2019-05-05 17:25:43 +02:00
|
|
|
/**
|
|
|
|
* Create an instance of a function call instruction
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* the function name that should be called
|
|
|
|
* @param javaCodePos
|
|
|
|
* the code position/offset in the Java method
|
|
|
|
* @param lineNumber
|
|
|
|
* the line number in the Java source code
|
2019-08-14 20:07:39 +02:00
|
|
|
* @param types
|
|
|
|
* the type manager
|
2019-10-05 16:15:18 +02:00
|
|
|
* @param options
|
|
|
|
* compiler properties
|
2019-05-05 17:25:43 +02:00
|
|
|
*/
|
2019-10-05 16:15:18 +02:00
|
|
|
WasmCallIndirectInstruction( FunctionName name, int javaCodePos, int lineNumber, TypeManager types, WasmOptions options ) {
|
2019-08-14 20:07:39 +02:00
|
|
|
super( name, javaCodePos, lineNumber, types );
|
|
|
|
this.type = types.valueOf( name.className );
|
2019-09-14 15:22:25 +02:00
|
|
|
this.options = options;
|
2019-05-05 17:25:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
Type getType() {
|
|
|
|
return Type.CallIndirect;
|
|
|
|
}
|
|
|
|
|
2019-10-05 16:15:18 +02:00
|
|
|
/**
|
|
|
|
* Get the type of this.
|
|
|
|
*
|
|
|
|
* @return the type
|
|
|
|
*/
|
|
|
|
StructType getThisType() {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the variable index on which this can be found.
|
|
|
|
* @param tempVarIdx the index
|
|
|
|
*/
|
|
|
|
void setVariableIndexOfThis( int tempVarIdx ) {
|
|
|
|
this.tempVarIdx = tempVarIdx;
|
|
|
|
}
|
|
|
|
|
2019-05-19 15:17:30 +02:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
2019-06-23 20:50:11 +02:00
|
|
|
void markAsNeeded( FunctionManager functions, boolean isStatic ) {
|
|
|
|
super.markAsNeeded( functions, isStatic );
|
2019-05-19 15:17:30 +02:00
|
|
|
virtualFunctionIdx = functions.getFunctionIndex( getFunctionName() );
|
|
|
|
}
|
|
|
|
|
2019-10-05 16:15:18 +02:00
|
|
|
/**
|
|
|
|
* if this call is executed virtual or if is was optimized.
|
|
|
|
*
|
|
|
|
* @return true, virtual call
|
|
|
|
*/
|
|
|
|
boolean isVirtual() {
|
|
|
|
return virtualFunctionIdx > 0;
|
|
|
|
}
|
|
|
|
|
2019-05-05 17:25:43 +02:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2019-05-14 21:47:49 +02:00
|
|
|
@Override
|
2019-05-05 17:25:43 +02:00
|
|
|
public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
|
2019-06-09 22:40:39 +02:00
|
|
|
if( virtualFunctionIdx < 0 ) {
|
2019-05-14 21:47:49 +02:00
|
|
|
super.writeTo( writer );
|
|
|
|
} else {
|
2019-09-13 20:04:03 +02:00
|
|
|
// duplicate this on the stack
|
|
|
|
writer.writeLocal( VariableOperator.get, tempVarIdx );
|
|
|
|
|
2019-09-14 15:22:25 +02:00
|
|
|
if( options.useGC() ) {
|
|
|
|
writer.writeStructOperator( StructOperator.GET, type, new NamedStorageType( type, "", "vtable" ), 0 ); // vtable is ever on position 0
|
|
|
|
} else {
|
|
|
|
writer.writeConst( 0, ValueType.i32 ); // vtable is ever on position 0
|
|
|
|
writer.writeFunctionCall( new JavaScriptSyntheticFunctionName( "NonGC", "get_i32", () -> "(a,i) => a[i]", ValueType.anyref, ValueType.i32, null, ValueType.i32 ) );
|
|
|
|
}
|
2019-11-18 20:08:18 +01:00
|
|
|
writer.writeMemoryOperator( MemoryOperator.load, ValueType.i32, virtualFunctionIdx * 4, 2 );
|
2019-09-13 20:04:03 +02:00
|
|
|
writer.writeVirtualFunctionCall( getFunctionName(), type );
|
2019-05-14 21:47:49 +02:00
|
|
|
}
|
2019-05-05 17:25:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
int getPopCount() {
|
2019-05-14 21:47:49 +02:00
|
|
|
return super.getPopCount() + 1; // this -> +1
|
2019-05-05 17:25:43 +02:00
|
|
|
}
|
|
|
|
}
|