2018-06-17 21:15:58 +02:00
|
|
|
/*
|
2019-01-20 19:58:23 +01:00
|
|
|
Copyright 2018 - 2019 Volker Berlin (i-net software)
|
2018-06-17 21:15:58 +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-08-06 12:52:44 +02:00
|
|
|
import javax.annotation.Nullable;
|
2018-06-17 21:15:58 +02:00
|
|
|
|
2019-01-20 19:58:23 +01:00
|
|
|
import de.inetsoftware.jwebassembly.wasm.AnyType;
|
2018-12-03 21:09:22 +01:00
|
|
|
|
2018-06-17 21:15:58 +02:00
|
|
|
/**
|
|
|
|
* Base class of all WasmInstruction.
|
|
|
|
*
|
|
|
|
* @author Volker Berlin
|
|
|
|
*
|
|
|
|
*/
|
2018-07-09 17:18:15 +02:00
|
|
|
abstract class WasmInstruction {
|
|
|
|
|
2018-12-16 18:22:44 +01:00
|
|
|
/**
|
|
|
|
* Type of instruction to faster differ as with instanceof.
|
|
|
|
*/
|
|
|
|
static enum Type {
|
2019-11-03 19:00:49 +01:00
|
|
|
Const, Convert, Local, Global, Table, Block, Numeric, Nop, Call, CallIndirect, Array, Struct, DupThis;
|
2018-12-16 18:22:44 +01:00
|
|
|
}
|
|
|
|
|
2019-03-31 11:23:45 +02:00
|
|
|
private int javaCodePos;
|
|
|
|
|
|
|
|
private final int lineNumber;
|
2018-07-09 17:18:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance of an instruction
|
|
|
|
*
|
|
|
|
* @param javaCodePos
|
|
|
|
* the code position/offset in the Java method
|
2019-03-31 11:23:45 +02:00
|
|
|
* @param lineNumber
|
|
|
|
* the line number in the Java source code
|
2018-07-09 17:18:15 +02:00
|
|
|
*/
|
2019-03-31 11:23:45 +02:00
|
|
|
WasmInstruction( int javaCodePos, int lineNumber ) {
|
2018-07-09 17:18:15 +02:00
|
|
|
this.javaCodePos = javaCodePos;
|
2019-03-31 11:23:45 +02:00
|
|
|
this.lineNumber = lineNumber;
|
2018-07-09 17:18:15 +02:00
|
|
|
}
|
2018-06-17 21:15:58 +02:00
|
|
|
|
2018-12-16 18:22:44 +01:00
|
|
|
/**
|
|
|
|
* Get the type of instruction
|
2019-03-31 11:23:45 +02:00
|
|
|
*
|
2018-12-16 18:22:44 +01:00
|
|
|
* @return the type
|
|
|
|
*/
|
|
|
|
@Nonnull
|
|
|
|
abstract Type getType();
|
|
|
|
|
2018-06-17 21:15:58 +02:00
|
|
|
/**
|
|
|
|
* Write this instruction to the WASM module.
|
|
|
|
*
|
|
|
|
* @param writer
|
|
|
|
* the target writer
|
|
|
|
* @throws IOException
|
|
|
|
* if any I/O error occur
|
|
|
|
*/
|
|
|
|
abstract void writeTo( @Nonnull ModuleWriter writer ) throws IOException;
|
2018-07-15 18:04:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get current code position in Java method.
|
2019-03-31 11:23:45 +02:00
|
|
|
*
|
2018-07-15 18:04:18 +02:00
|
|
|
* @return the position
|
|
|
|
*/
|
|
|
|
int getCodePosition() {
|
|
|
|
return javaCodePos;
|
|
|
|
}
|
2018-08-02 12:12:51 +02:00
|
|
|
|
2019-03-31 11:23:45 +02:00
|
|
|
/**
|
|
|
|
* Get the line number in the Java source file
|
|
|
|
*
|
|
|
|
* @return the line number
|
|
|
|
*/
|
|
|
|
int getLineNumber() {
|
|
|
|
return lineNumber;
|
|
|
|
}
|
|
|
|
|
2018-08-02 12:12:51 +02:00
|
|
|
/**
|
|
|
|
* Set a new code position after reorganize the order
|
2019-03-31 11:23:45 +02:00
|
|
|
*
|
|
|
|
* @param newPos
|
|
|
|
* new position
|
2018-08-02 12:12:51 +02:00
|
|
|
*/
|
|
|
|
void setCodePosition( int newPos ) {
|
2019-03-31 11:23:45 +02:00
|
|
|
this.javaCodePos = newPos;
|
2018-08-02 12:12:51 +02:00
|
|
|
}
|
2018-08-06 12:52:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ValueType if this instruction push a value on the stack.
|
|
|
|
*
|
|
|
|
* @return the ValueType or null if no value is push
|
|
|
|
*/
|
|
|
|
@Nullable
|
2019-01-20 19:58:23 +01:00
|
|
|
abstract AnyType getPushValueType();
|
2018-08-10 18:25:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the count of values that are removed from the stack.
|
|
|
|
*
|
|
|
|
* @return the count
|
|
|
|
*/
|
|
|
|
abstract int getPopCount();
|
2018-06-17 21:15:58 +02:00
|
|
|
}
|