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

115 lines
2.6 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;
2019-01-18 17:48:12 +01:00
import java.util.Iterator;
import javax.annotation.Nonnull;
2019-01-18 17:48:12 +01:00
import de.inetsoftware.jwebassembly.wasm.AnyType;
2018-12-03 21:09:22 +01:00
import de.inetsoftware.jwebassembly.wasm.ValueType;
/**
* WasmInstruction for a function call.
*
* @author Volker Berlin
*
*/
class WasmCallInstruction extends WasmInstruction {
private ValueType valueType;
private final FunctionName name;
private int paramCount = -1;
/**
* 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
*/
WasmCallInstruction( FunctionName name, int javaCodePos ) {
super( javaCodePos );
this.name = name;
}
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}
*/
AnyType 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;
}
Iterator<AnyType> parser = name.getSignature();
paramCount = 0;
while( parser.next() != null ) {
2018-08-10 18:25:38 +02:00
paramCount++;
}
valueType = (ValueType)parser.next();
2019-01-18 17:48:12 +01:00
while( parser.hasNext() ) {
valueType = (ValueType)parser.next();
paramCount--;
}
2018-08-10 18:25:38 +02:00
}
}