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

84 lines
2.1 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;
/**
* 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-05-18 21:37:19 +02:00
private int virtualFunctionIdx = -1;
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
*/
WasmCallIndirectInstruction( FunctionName name, int javaCodePos, int lineNumber ) {
2019-05-14 21:47:49 +02:00
super( name, javaCodePos, lineNumber );
2019-05-05 17:25:43 +02:00
}
/**
* {@inheritDoc}
*/
@Override
Type getType() {
return Type.CallIndirect;
}
/**
* {@inheritDoc}
*/
@Override
void markAsNeeded( FunctionManager functions ) {
super.markAsNeeded( functions );
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-05-18 21:37:19 +02:00
if( virtualFunctionIdx < 0 ) {
2019-05-14 21:47:49 +02:00
super.writeTo( writer );
} else {
2019-05-18 21:37:19 +02:00
writer.writeVirtualFunctionCall( getFunctionName(), virtualFunctionIdx );
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
}
}