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

125 lines
4.2 KiB
Java
Raw Normal View History

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;
import de.inetsoftware.jwebassembly.module.TypeManager.StructType;
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;
import de.inetsoftware.jwebassembly.wasm.VariableOperator;
2019-09-14 15:22:25 +02:00
import de.inetsoftware.jwebassembly.wasm.WasmOptions;
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
private int virtualFunctionIdx = -1;
2019-05-18 21:37:19 +02:00
private final StructType type;
/**
* the slot of a temporary variable of type "type" to duplicate "this"
*/
private final int tempVar;
private final LocaleVariableManager localVariables;
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 localVariables
* the manager for local variables to translate the Java slot of the temporary variable into wasm local
* position
2019-05-05 17:25:43 +02:00
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
* @param types
* the type manager
2019-05-05 17:25:43 +02:00
*/
2019-09-14 15:22:25 +02:00
WasmCallIndirectInstruction( FunctionName name, LocaleVariableManager localVariables, int javaCodePos, int lineNumber, TypeManager types, WasmOptions options ) {
super( name, javaCodePos, lineNumber, types );
this.type = types.valueOf( name.className );
this.tempVar = localVariables.getTempVariable( type, javaCodePos, javaCodePos + 1 );
this.localVariables = localVariables;
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;
}
/**
* {@inheritDoc}
*/
@Override
void markAsNeeded( FunctionManager functions, boolean isStatic ) {
super.markAsNeeded( functions, isStatic );
virtualFunctionIdx = functions.getFunctionIndex( getFunctionName() );
}
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 {
int tempVarIdx = localVariables.get( tempVar, getCodePosition() );
// duplicate this on the stack
writer.writeLocal( VariableOperator.tee, tempVarIdx );
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 ) );
}
writer.writeLoadI32( virtualFunctionIdx * 4 );
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
}
}