mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-25 23:47:51 +01:00
Add table.set/get
This commit is contained in:
parent
b47274e007
commit
baf7fb9cf0
@ -16,7 +16,6 @@
|
|||||||
package de.inetsoftware.jwebassembly.binary;
|
package de.inetsoftware.jwebassembly.binary;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
@ -24,6 +23,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnegative;
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
@ -648,6 +648,15 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
|||||||
codeStream.writeVaruint32( var.id );
|
codeStream.writeVaruint32( var.id );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void writeTable( boolean load, @Nonnegative int idx ) throws IOException {
|
||||||
|
codeStream.writeOpCode( load ? TABLE_GET : TABLE_SET );
|
||||||
|
codeStream.writeVaruint32( idx );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
|
@ -18,6 +18,7 @@ package de.inetsoftware.jwebassembly.module;
|
|||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnegative;
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
@ -195,7 +196,7 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
protected abstract void writeLocal( VariableOperator op, int idx ) throws IOException;
|
protected abstract void writeLocal( VariableOperator op, int idx ) throws IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write a set_global variable
|
* Write a global variable operation
|
||||||
* @param load
|
* @param load
|
||||||
* true: if load or GET
|
* true: if load or GET
|
||||||
* @param name
|
* @param name
|
||||||
@ -208,6 +209,18 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
*/
|
*/
|
||||||
protected abstract void writeGlobalAccess( boolean load, FunctionName name, AnyType type ) throws IOException;
|
protected abstract void writeGlobalAccess( boolean load, FunctionName name, AnyType type ) throws IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a table operation.
|
||||||
|
* @param load
|
||||||
|
* true: if "get" else "set"
|
||||||
|
* @param idx
|
||||||
|
* the index of the table
|
||||||
|
*
|
||||||
|
* @throws IOException
|
||||||
|
* if any I/O error occur
|
||||||
|
*/
|
||||||
|
protected abstract void writeTable( boolean load, @Nonnegative int idx ) throws IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write the default/initial value for a type.
|
* Write the default/initial value for a type.
|
||||||
*
|
*
|
||||||
|
@ -252,7 +252,7 @@ public abstract class WasmCodeBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a WasmLoadStoreInstruction get_local/set_local.
|
* Create a WasmLoadStoreInstruction local.get/local.set.
|
||||||
*
|
*
|
||||||
* @param load
|
* @param load
|
||||||
* true: if load
|
* true: if load
|
||||||
@ -286,6 +286,23 @@ public abstract class WasmCodeBuilder {
|
|||||||
instructions.add( new WasmGlobalInstruction( load, name, type, javaCodePos, lineNumber ) );
|
instructions.add( new WasmGlobalInstruction( load, name, type, javaCodePos, lineNumber ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a WasmTableInstruction table.get/table.set.
|
||||||
|
*
|
||||||
|
* @param load
|
||||||
|
* true: if load
|
||||||
|
* @param idx
|
||||||
|
* the index of the table
|
||||||
|
* @param javaCodePos
|
||||||
|
* the code position/offset in the Java method
|
||||||
|
* @param lineNumber
|
||||||
|
* the line number in the Java source code
|
||||||
|
*/
|
||||||
|
@Nonnull
|
||||||
|
protected void addTableInstruction( boolean load, @Nonnegative int idx, int javaCodePos, int lineNumber ) {
|
||||||
|
instructions.add( new WasmTableInstruction( load, idx, javaCodePos, lineNumber ) );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a constant instruction.
|
* Add a constant instruction.
|
||||||
*
|
*
|
||||||
@ -319,6 +336,7 @@ public abstract class WasmCodeBuilder {
|
|||||||
strings.put( (String)value, id = strings.size() );
|
strings.put( (String)value, id = strings.size() );
|
||||||
}
|
}
|
||||||
FunctionName name = getNonGC( "stringConstant", lineNumber );
|
FunctionName name = getNonGC( "stringConstant", lineNumber );
|
||||||
|
instructions.add( new WasmConstInstruction( id, ValueType.i32, javaCodePos, lineNumber ) );
|
||||||
addCallInstruction( name, javaCodePos, lineNumber );
|
addCallInstruction( name, javaCodePos, lineNumber );
|
||||||
} else {
|
} else {
|
||||||
instructions.add( new WasmConstInstruction( (Number)value, javaCodePos, lineNumber ) );
|
instructions.add( new WasmConstInstruction( (Number)value, javaCodePos, lineNumber ) );
|
||||||
|
@ -35,7 +35,7 @@ abstract class WasmInstruction {
|
|||||||
* Type of instruction to faster differ as with instanceof.
|
* Type of instruction to faster differ as with instanceof.
|
||||||
*/
|
*/
|
||||||
static enum Type {
|
static enum Type {
|
||||||
Const, Convert, Local, Global, Block, Numeric, Nop, Call, CallIndirect, Array, Struct, DupThis;
|
Const, Convert, Local, Global, Table, Block, Numeric, Nop, Call, CallIndirect, Array, Struct, DupThis;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int javaCodePos;
|
private int javaCodePos;
|
||||||
|
@ -0,0 +1,88 @@
|
|||||||
|
/*
|
||||||
|
Copyright 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.Nonnegative;
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
|
import de.inetsoftware.jwebassembly.wasm.AnyType;
|
||||||
|
import de.inetsoftware.jwebassembly.wasm.ValueType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WasmInstruction for load and store a element in a table.
|
||||||
|
*
|
||||||
|
* @author Volker Berlin
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class WasmTableInstruction extends WasmInstruction {
|
||||||
|
|
||||||
|
private boolean load;
|
||||||
|
|
||||||
|
private int idx;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an instance of a load/store instruction
|
||||||
|
*
|
||||||
|
* @param load
|
||||||
|
* true: if "get" else "set"
|
||||||
|
* @param idx
|
||||||
|
* the index of the table
|
||||||
|
* @param javaCodePos
|
||||||
|
* the code position/offset in the Java method
|
||||||
|
* @param lineNumber
|
||||||
|
* the line number in the Java source code
|
||||||
|
*/
|
||||||
|
WasmTableInstruction( boolean load, @Nonnegative int idx, int javaCodePos, int lineNumber ) {
|
||||||
|
super( javaCodePos, lineNumber );
|
||||||
|
this.load = load;
|
||||||
|
this.idx = idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
Type getType() {
|
||||||
|
return Type.Table;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
|
||||||
|
writer.writeTable( load, idx );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
AnyType getPushValueType() {
|
||||||
|
return load ? ValueType.anyref : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
int getPopCount() {
|
||||||
|
return load ? 0 : 1;
|
||||||
|
}
|
||||||
|
}
|
@ -25,6 +25,7 @@ import java.util.Map;
|
|||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnegative;
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
@ -470,6 +471,15 @@ public class TextModuleWriter extends ModuleWriter {
|
|||||||
methodOutput.append( load ? "global.get $" : "global.set $" ).append( fullName );
|
methodOutput.append( load ? "global.get $" : "global.set $" ).append( fullName );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void writeTable( boolean load, @Nonnegative int idx ) throws IOException {
|
||||||
|
newline( methodOutput );
|
||||||
|
methodOutput.append( load ? "table.get " : "table.set " ).append( idx );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
|
@ -171,6 +171,12 @@ public class WatParser extends WasmCodeBuilder {
|
|||||||
case "f64.trunc":
|
case "f64.trunc":
|
||||||
addNumericInstruction( NumericOperator.trunc, ValueType.f64, javaCodePos, lineNumber );
|
addNumericInstruction( NumericOperator.trunc, ValueType.f64, javaCodePos, lineNumber );
|
||||||
break;
|
break;
|
||||||
|
case "table.get":
|
||||||
|
addTableInstruction( true, getInt( tokens, ++i), javaCodePos, lineNumber );
|
||||||
|
break;
|
||||||
|
case "table.set":
|
||||||
|
addTableInstruction( false, getInt( tokens, ++i), javaCodePos, lineNumber );
|
||||||
|
break;
|
||||||
// case "call":
|
// case "call":
|
||||||
// addCallInstruction( method, javaCodePos );
|
// addCallInstruction( method, javaCodePos );
|
||||||
// break;
|
// break;
|
||||||
|
@ -279,6 +279,16 @@ public class WatParserTest {
|
|||||||
test( "if (result i32) else end" );
|
test( "if (result i32) else end" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void table_get() throws IOException {
|
||||||
|
test( "table.get 1" );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void table_set() throws IOException {
|
||||||
|
test( "table.set 2" );
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void errorMissingToken() throws IOException {
|
public void errorMissingToken() throws IOException {
|
||||||
testError( "i32.const", "Missing Token in wasm text format after token: i32.const" );
|
testError( "i32.const", "Missing Token in wasm text format after token: i32.const" );
|
||||||
|
Loading…
x
Reference in New Issue
Block a user