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

119 lines
2.8 KiB
Java
Raw Normal View History

/*
Copyright 2018 - 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;
import de.inetsoftware.classparser.Member;
2018-12-03 21:09:22 +01:00
import de.inetsoftware.jwebassembly.wasm.ValueType;
import de.inetsoftware.jwebassembly.wasm.ValueTypeParser;
/**
* WasmInstruction for a function call.
*
* @author Volker Berlin
*
*/
class WasmCallInstruction extends WasmInstruction {
private final Member method;
2018-08-06 12:52:44 +02:00
private ValueType valueType;
private final FunctionName name;
private int paramCount = -1;
/**
* Create an instance of a function call instruction
*
* @param method
* the reference to the Java method
* @param javaCodePos
* the code position/offset in the Java method
*/
WasmCallInstruction( Member method, int javaCodePos ) {
super( javaCodePos );
2018-08-10 18:25:38 +02:00
this.method = method;
this.name = new FunctionName( method );
}
2018-12-16 18:22:44 +01:00
/**
* {@inheritDoc}
*/
@Override
Type getType() {
return Type.Call;
}
/**
* Get the function name that should be called
*
* @return the name
*/
@Nonnull
FunctionName getFunctionName() {
return name;
}
/**
* {@inheritDoc}
*/
public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
writer.writeFunctionCall( name );
}
2018-08-06 12:52:44 +02:00
/**
* {@inheritDoc}
*/
ValueType getPushValueType() {
countParams();
2018-08-06 12:52:44 +02:00
return valueType;
}
2018-08-10 18:25:38 +02:00
/**
* {@inheritDoc}
*/
@Override
int getPopCount() {
countParams();
return paramCount;
}
/**
* Count the parameters in the signature
*/
private void countParams() {
if( paramCount >= 0 ) {
return;
}
ValueTypeParser parser = new ValueTypeParser( method.getType() );
paramCount = 0;
while( parser.next() != null ) {
2018-08-10 18:25:38 +02:00
paramCount++;
}
valueType = (ValueType)parser.next();
ValueType type;
while( (type = (ValueType)parser.next()) != null ) {
valueType = type;
paramCount--;
}
2018-08-10 18:25:38 +02:00
}
}