2018-06-21 18:29:20 +02:00
|
|
|
/*
|
2019-01-11 22:21:59 +01:00
|
|
|
Copyright 2018 - 2019 Volker Berlin (i-net software)
|
2018-06-21 18:29:20 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-11-10 10:46:36 +01:00
|
|
|
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;
|
2018-08-06 12:46:18 +02:00
|
|
|
|
2018-06-21 18:29:20 +02:00
|
|
|
/**
|
|
|
|
* WasmInstruction for a function call.
|
|
|
|
*
|
|
|
|
* @author Volker Berlin
|
|
|
|
*
|
|
|
|
*/
|
2018-07-09 17:18:15 +02:00
|
|
|
class WasmCallInstruction extends WasmInstruction {
|
2018-06-21 18:29:20 +02:00
|
|
|
|
2018-11-29 18:35:56 +01:00
|
|
|
private final Member method;
|
2018-08-06 12:52:44 +02:00
|
|
|
|
2018-11-29 18:35:56 +01:00
|
|
|
private ValueType valueType;
|
2018-06-21 18:29:20 +02:00
|
|
|
|
2018-11-24 21:09:56 +01:00
|
|
|
private final FunctionName name;
|
|
|
|
|
2018-11-29 18:35:56 +01:00
|
|
|
private int paramCount = -1;
|
|
|
|
|
2018-06-21 18:29:20 +02:00
|
|
|
/**
|
2018-07-09 17:18:15 +02:00
|
|
|
* Create an instance of a function call instruction
|
|
|
|
*
|
2018-08-06 12:46:18 +02:00
|
|
|
* @param method
|
|
|
|
* the reference to the Java method
|
2018-07-09 17:18:15 +02:00
|
|
|
* @param javaCodePos
|
|
|
|
* the code position/offset in the Java method
|
2018-06-21 18:29:20 +02:00
|
|
|
*/
|
2018-11-10 10:46:36 +01:00
|
|
|
WasmCallInstruction( Member method, int javaCodePos ) {
|
2018-07-09 17:18:15 +02:00
|
|
|
super( javaCodePos );
|
2018-08-10 18:25:38 +02:00
|
|
|
this.method = method;
|
2018-11-24 21:09:56 +01:00
|
|
|
this.name = new FunctionName( method );
|
2018-06-21 18:29:20 +02:00
|
|
|
}
|
|
|
|
|
2018-12-16 18:22:44 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
Type getType() {
|
|
|
|
return Type.Call;
|
|
|
|
}
|
|
|
|
|
2018-11-24 21:09:56 +01:00
|
|
|
/**
|
|
|
|
* Get the function name that should be called
|
2018-11-29 18:35:56 +01:00
|
|
|
*
|
2018-11-24 21:09:56 +01:00
|
|
|
* @return the name
|
|
|
|
*/
|
|
|
|
@Nonnull
|
|
|
|
FunctionName getFunctionName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2018-06-21 18:29:20 +02:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
|
2018-11-24 21:09:56 +01:00
|
|
|
writer.writeFunctionCall( name );
|
2018-06-21 18:29:20 +02:00
|
|
|
}
|
2018-08-06 12:52:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
ValueType getPushValueType() {
|
2018-11-29 18:35:56 +01:00
|
|
|
countParams();
|
2018-08-06 12:52:44 +02:00
|
|
|
return valueType;
|
|
|
|
}
|
2018-08-10 18:25:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
int getPopCount() {
|
2018-11-29 18:35:56 +01:00
|
|
|
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++;
|
|
|
|
}
|
2019-01-11 22:21:59 +01:00
|
|
|
valueType = (ValueType)parser.next();
|
2018-11-29 18:35:56 +01:00
|
|
|
ValueType type;
|
2019-01-11 22:21:59 +01:00
|
|
|
while( (type = (ValueType)parser.next()) != null ) {
|
2018-11-29 18:35:56 +01:00
|
|
|
valueType = type;
|
|
|
|
paramCount--;
|
|
|
|
}
|
2018-08-10 18:25:38 +02:00
|
|
|
}
|
2018-06-21 18:29:20 +02:00
|
|
|
}
|