add getType() to WasmInstruction

This commit is contained in:
Volker Berlin 2018-12-16 18:22:44 +01:00
parent 5d237c2ae1
commit bc2975683a
13 changed files with 117 additions and 2 deletions

View File

@ -233,8 +233,11 @@ public class ModuleGenerator {
writeMethodSignature( name, method.isStatic(), localVariableTable, codeBuilder ); writeMethodSignature( name, method.isStatic(), localVariableTable, codeBuilder );
for( WasmInstruction instruction : codeBuilder.getInstructions() ) { for( WasmInstruction instruction : codeBuilder.getInstructions() ) {
if( instruction instanceof WasmCallInstruction ) { switch( instruction.getType() ) {
functions.functionCall( ((WasmCallInstruction)instruction).getFunctionName() ); case Call:
functions.functionCall( ((WasmCallInstruction)instruction).getFunctionName() );
break;
default:
} }
instruction.writeTo( writer ); instruction.writeTo( writer );
} }

View File

@ -22,6 +22,7 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import de.inetsoftware.jwebassembly.WasmException; import de.inetsoftware.jwebassembly.WasmException;
import de.inetsoftware.jwebassembly.module.WasmInstruction.Type;
import de.inetsoftware.jwebassembly.wasm.ArrayOperator; import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
import de.inetsoftware.jwebassembly.wasm.StorageType; import de.inetsoftware.jwebassembly.wasm.StorageType;
import de.inetsoftware.jwebassembly.wasm.ValueType; import de.inetsoftware.jwebassembly.wasm.ValueType;
@ -54,6 +55,14 @@ class WasmArrayInstruction extends WasmInstruction {
this.type = type; this.type = type;
} }
/**
* {@inheritDoc}
*/
@Override
Type getType() {
return Type.Array;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View File

@ -21,6 +21,7 @@ import java.io.IOException;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import de.inetsoftware.jwebassembly.module.WasmInstruction.Type;
import de.inetsoftware.jwebassembly.wasm.ValueType; import de.inetsoftware.jwebassembly.wasm.ValueType;
import de.inetsoftware.jwebassembly.wasm.WasmBlockOperator; import de.inetsoftware.jwebassembly.wasm.WasmBlockOperator;
@ -52,6 +53,14 @@ class WasmBlockInstruction extends WasmInstruction {
this.data = data; this.data = data;
} }
/**
* {@inheritDoc}
*/
@Override
Type getType() {
return Type.Block;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View File

@ -54,6 +54,14 @@ class WasmCallInstruction extends WasmInstruction {
this.name = new FunctionName( method ); this.name = new FunctionName( method );
} }
/**
* {@inheritDoc}
*/
@Override
Type getType() {
return Type.Call;
}
/** /**
* Get the function name that should be called * Get the function name that should be called
* *

View File

@ -21,6 +21,7 @@ import java.io.IOException;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import de.inetsoftware.jwebassembly.WasmException; import de.inetsoftware.jwebassembly.WasmException;
import de.inetsoftware.jwebassembly.module.WasmInstruction.Type;
import de.inetsoftware.jwebassembly.wasm.ValueType; import de.inetsoftware.jwebassembly.wasm.ValueType;
/** /**
@ -63,6 +64,14 @@ class WasmConstInstruction extends WasmInstruction {
this( value, getValueType( value ), javaCodePos ); this( value, getValueType( value ), javaCodePos );
} }
/**
* {@inheritDoc}
*/
@Override
Type getType() {
return Type.Const;
}
/** /**
* Find the matching ValueType for the given value. * Find the matching ValueType for the given value.
* *

View File

@ -17,6 +17,7 @@ package de.inetsoftware.jwebassembly.module;
import java.io.IOException; import java.io.IOException;
import de.inetsoftware.jwebassembly.module.WasmInstruction.Type;
import de.inetsoftware.jwebassembly.wasm.ValueType; import de.inetsoftware.jwebassembly.wasm.ValueType;
/** /**
@ -42,6 +43,14 @@ class WasmConvertInstruction extends WasmInstruction {
this.conversion = conversion; this.conversion = conversion;
} }
/**
* {@inheritDoc}
*/
@Override
Type getType() {
return Type.Convert;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View File

@ -21,6 +21,7 @@ import java.io.IOException;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import de.inetsoftware.classparser.Member; import de.inetsoftware.classparser.Member;
import de.inetsoftware.jwebassembly.module.WasmInstruction.Type;
import de.inetsoftware.jwebassembly.wasm.ValueType; import de.inetsoftware.jwebassembly.wasm.ValueType;
/** /**
@ -51,6 +52,14 @@ class WasmGlobalInstruction extends WasmInstruction {
this.ref = ref; this.ref = ref;
} }
/**
* {@inheritDoc}
*/
@Override
Type getType() {
return Type.Global;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View File

@ -31,6 +31,13 @@ import de.inetsoftware.jwebassembly.wasm.ValueType;
*/ */
abstract class WasmInstruction { abstract class WasmInstruction {
/**
* Type of instruction to faster differ as with instanceof.
*/
static enum Type {
LoadStore, Const, Convert, Local, Global, Block, Numeric, Nop, Call, Array, Struct;
}
private int javaCodePos; private int javaCodePos;
/** /**
@ -43,6 +50,13 @@ abstract class WasmInstruction {
this.javaCodePos = javaCodePos; this.javaCodePos = javaCodePos;
} }
/**
* Get the type of instruction
* @return the type
*/
@Nonnull
abstract Type getType();
/** /**
* Write this instruction to the WASM module. * Write this instruction to the WASM module.
* *

View File

@ -21,6 +21,7 @@ import java.io.IOException;
import javax.annotation.Nonnegative; import javax.annotation.Nonnegative;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import de.inetsoftware.jwebassembly.module.WasmInstruction.Type;
import de.inetsoftware.jwebassembly.wasm.ValueType; import de.inetsoftware.jwebassembly.wasm.ValueType;
/** /**
@ -56,6 +57,14 @@ class WasmLoadStoreInstruction extends WasmInstruction {
this.localVariables = localVariables; this.localVariables = localVariables;
} }
/**
* {@inheritDoc}
*/
@Override
Type getType() {
return Type.LoadStore;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View File

@ -21,6 +21,7 @@ import java.io.IOException;
import javax.annotation.Nonnegative; import javax.annotation.Nonnegative;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import de.inetsoftware.jwebassembly.module.WasmInstruction.Type;
import de.inetsoftware.jwebassembly.wasm.ValueType; import de.inetsoftware.jwebassembly.wasm.ValueType;
/** /**
@ -51,6 +52,14 @@ class WasmLocalInstruction extends WasmInstruction {
this.idx = idx; this.idx = idx;
} }
/**
* {@inheritDoc}
*/
@Override
Type getType() {
return Type.Local;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View File

@ -20,6 +20,7 @@ import java.io.IOException;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import de.inetsoftware.jwebassembly.module.WasmInstruction.Type;
import de.inetsoftware.jwebassembly.wasm.ValueType; import de.inetsoftware.jwebassembly.wasm.ValueType;
/** /**
@ -40,6 +41,14 @@ class WasmNopInstruction extends WasmInstruction {
super( javaCodePos ); super( javaCodePos );
} }
/**
* {@inheritDoc}
*/
@Override
Type getType() {
return Type.Nop;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View File

@ -21,6 +21,7 @@ import java.io.IOException;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import de.inetsoftware.jwebassembly.module.WasmInstruction.Type;
import de.inetsoftware.jwebassembly.wasm.NumericOperator; import de.inetsoftware.jwebassembly.wasm.NumericOperator;
import de.inetsoftware.jwebassembly.wasm.ValueType; import de.inetsoftware.jwebassembly.wasm.ValueType;
@ -52,6 +53,14 @@ class WasmNumericInstruction extends WasmInstruction {
this.valueType = valueType; this.valueType = valueType;
} }
/**
* {@inheritDoc}
*/
@Override
Type getType() {
return Type.Numeric;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View File

@ -22,6 +22,7 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import de.inetsoftware.jwebassembly.WasmException; import de.inetsoftware.jwebassembly.WasmException;
import de.inetsoftware.jwebassembly.module.WasmInstruction.Type;
import de.inetsoftware.jwebassembly.wasm.ArrayOperator; import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
import de.inetsoftware.jwebassembly.wasm.StorageType; import de.inetsoftware.jwebassembly.wasm.StorageType;
import de.inetsoftware.jwebassembly.wasm.StructOperator; import de.inetsoftware.jwebassembly.wasm.StructOperator;
@ -55,6 +56,14 @@ class WasmStructInstruction extends WasmInstruction {
this.type = type; this.type = type;
} }
/**
* {@inheritDoc}
*/
@Override
Type getType() {
return Type.Struct;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */