From f5ed8aeeb6b3e47f1593d6583db82cc0d8a1f380 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Wed, 20 Feb 2019 21:42:52 +0100 Subject: [PATCH] get_local --> local.get, set_local --> local.set, see #3 --- .../binary/BinaryModuleWriter.java | 25 +++++++++-------- .../binary/InstructionOpcodes.java | 8 +++--- .../module/JavaMethodWasmCodeBuilder.java | 2 +- .../jwebassembly/module/ModuleWriter.java | 17 ++++------- .../module/WasmLocalInstruction.java | 16 +++++------ .../jwebassembly/text/TextModuleWriter.java | 14 ++-------- .../jwebassembly/wasm/VariableOperator.java | 28 +++++++++++++++++++ .../jwebassembly/watparser/WatParser.java | 6 ++-- .../jwebassembly/module/WatParserTest.java | 10 +++---- .../jwebassembly/runtime/CallFunctions.java | 6 ++-- .../samples/FunctionParameters.wat | 4 +-- 11 files changed, 75 insertions(+), 61 deletions(-) create mode 100644 src/de/inetsoftware/jwebassembly/wasm/VariableOperator.java diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index 5f6f887..f158f6d 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -40,6 +40,7 @@ import de.inetsoftware.jwebassembly.wasm.NumericOperator; import de.inetsoftware.jwebassembly.wasm.AnyType; import de.inetsoftware.jwebassembly.wasm.StructOperator; import de.inetsoftware.jwebassembly.wasm.ValueType; +import de.inetsoftware.jwebassembly.wasm.VariableOperator; import de.inetsoftware.jwebassembly.wasm.WasmBlockOperator; /** @@ -350,17 +351,19 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod * {@inheritDoc} */ @Override - protected void writeLoad( int idx ) throws IOException { - codeStream.writeOpCode( GET_LOCAL ); - codeStream.writeVaruint32( idx ); - } - - /** - * {@inheritDoc} - */ - @Override - protected void writeStore( int idx ) throws IOException { - codeStream.writeOpCode( SET_LOCAL ); + protected void writeLocal( VariableOperator op, int idx ) throws IOException { + int code; + switch( op ) { + case get: + code = LOCAL_GET; + break; + case set: + code = LOCAL_SET; + break; + default: + code = LOCAL_TEE; + } + codeStream.writeOpCode( code ); codeStream.writeVaruint32( idx ); } diff --git a/src/de/inetsoftware/jwebassembly/binary/InstructionOpcodes.java b/src/de/inetsoftware/jwebassembly/binary/InstructionOpcodes.java index 536be0a..f9f11cd 100644 --- a/src/de/inetsoftware/jwebassembly/binary/InstructionOpcodes.java +++ b/src/de/inetsoftware/jwebassembly/binary/InstructionOpcodes.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 - 2018 Volker Berlin (i-net software) + * Copyright 2017 - 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. @@ -90,11 +90,11 @@ interface InstructionOpcodes { // === Variable access =========== - static final int GET_LOCAL = 0x20; + static final int LOCAL_GET = 0x20; - static final int SET_LOCAL = 0x21; + static final int LOCAL_SET = 0x21; - static final int TEE_LOCAL = 0x22; + static final int LOCAL_TEE = 0x22; static final int GET_GLOBAL = 0x23; diff --git a/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java index 13477f3..b4bd64d 100644 --- a/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java @@ -276,7 +276,7 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder { case 89: // dup: duplicate the value on top of the stack case 92: // dup2 storeType = findPreviousPushInstructionPushValueType(); - addCallInstruction( new SyntheticFunctionName( "dup" + storeType, "get_local 0 get_local 0 return", storeType, null, storeType, storeType ), codePos ); + addCallInstruction( new SyntheticFunctionName( "dup" + storeType, "local.get 0 local.get 0 return", storeType, null, storeType, storeType ), codePos ); break; case 90: // dup_x1 case 91: // dup_x2 diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java index ac5da4a..4cd1c6d 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java @@ -29,6 +29,7 @@ import de.inetsoftware.jwebassembly.wasm.NumericOperator; import de.inetsoftware.jwebassembly.wasm.AnyType; import de.inetsoftware.jwebassembly.wasm.StructOperator; import de.inetsoftware.jwebassembly.wasm.ValueType; +import de.inetsoftware.jwebassembly.wasm.VariableOperator; import de.inetsoftware.jwebassembly.wasm.WasmBlockOperator; /** @@ -138,24 +139,16 @@ public abstract class ModuleWriter implements Closeable { protected abstract void writeConst( Number value, ValueType valueType ) throws IOException; /** - * Write a variable load. + * Write a local variable operation. * + * @param op + * the operation * @param idx * the index of the parameter variable * @throws IOException * if any I/O error occur */ - protected abstract void writeLoad( int idx ) throws IOException; - - /** - * Write a variable store. - * - * @param idx - * the index of the parameter variable - * @throws IOException - * if any I/O error occur - */ - protected abstract void writeStore( int idx ) throws IOException; + protected abstract void writeLocal( VariableOperator op, int idx ) throws IOException; /** * Write a set_global variable diff --git a/src/de/inetsoftware/jwebassembly/module/WasmLocalInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmLocalInstruction.java index 9e8eb39..15f2e86 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmLocalInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmLocalInstruction.java @@ -21,8 +21,10 @@ import java.io.IOException; import javax.annotation.Nonnegative; import javax.annotation.Nonnull; -import de.inetsoftware.jwebassembly.module.WasmInstruction.Type; import de.inetsoftware.jwebassembly.wasm.AnyType; +import de.inetsoftware.jwebassembly.wasm.VariableOperator; + +import static de.inetsoftware.jwebassembly.wasm.VariableOperator.*; /** * WasmInstruction for load and store local variables. @@ -32,7 +34,7 @@ import de.inetsoftware.jwebassembly.wasm.AnyType; */ class WasmLocalInstruction extends WasmInstruction { - private boolean load; + private VariableOperator op; private int idx; @@ -48,7 +50,7 @@ class WasmLocalInstruction extends WasmInstruction { */ WasmLocalInstruction( boolean load, @Nonnegative int idx, int javaCodePos ) { super( javaCodePos ); - this.load = load; + this.op = load ? get : set; this.idx = idx; } @@ -75,11 +77,7 @@ class WasmLocalInstruction extends WasmInstruction { */ public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { int index = getIndex(); - if( load ) { - writer.writeLoad( index ); - } else { - writer.writeStore( index ); - } + writer.writeLocal( op, index ); } /** @@ -94,6 +92,6 @@ class WasmLocalInstruction extends WasmInstruction { */ @Override int getPopCount() { - return load ? 0 : 1; + return op == get ? 0 : 1; } } diff --git a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java index 7ff6023..571ff0c 100644 --- a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java @@ -34,6 +34,7 @@ import de.inetsoftware.jwebassembly.wasm.NumericOperator; import de.inetsoftware.jwebassembly.wasm.AnyType; import de.inetsoftware.jwebassembly.wasm.StructOperator; import de.inetsoftware.jwebassembly.wasm.ValueType; +import de.inetsoftware.jwebassembly.wasm.VariableOperator; import de.inetsoftware.jwebassembly.wasm.WasmBlockOperator; /** @@ -194,18 +195,9 @@ public class TextModuleWriter extends ModuleWriter { * {@inheritDoc} */ @Override - protected void writeLoad( int idx ) throws IOException { + protected void writeLocal( VariableOperator op, int idx ) throws IOException { newline( methodOutput ); - methodOutput.append( "get_local " ).append( Integer.toString( idx ) ); - } - - /** - * {@inheritDoc} - */ - @Override - protected void writeStore( int idx ) throws IOException { - newline( methodOutput ); - methodOutput.append( "set_local " ).append( Integer.toString( idx ) ); + methodOutput.append( "local." ).append( op ).append( ' ' ).append( Integer.toString( idx ) ); } /** diff --git a/src/de/inetsoftware/jwebassembly/wasm/VariableOperator.java b/src/de/inetsoftware/jwebassembly/wasm/VariableOperator.java new file mode 100644 index 0000000..017bfb2 --- /dev/null +++ b/src/de/inetsoftware/jwebassembly/wasm/VariableOperator.java @@ -0,0 +1,28 @@ +/* + 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.wasm; + +/** + * Operation on Variables. + * + * @author Volker Berlin + */ +public enum VariableOperator { + get, + set, + tee, +} \ No newline at end of file diff --git a/src/de/inetsoftware/jwebassembly/watparser/WatParser.java b/src/de/inetsoftware/jwebassembly/watparser/WatParser.java index ce38fb6..85234fa 100644 --- a/src/de/inetsoftware/jwebassembly/watparser/WatParser.java +++ b/src/de/inetsoftware/jwebassembly/watparser/WatParser.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Volker Berlin (i-net software) + 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. @@ -55,10 +55,10 @@ public class WatParser extends WasmCodeBuilder { int javaCodePos = i; String tok = tokens.get( i ); switch( tok ) { - case "get_local": + case "local.get": addLocalInstruction( true, getInt( tokens, ++i), javaCodePos ); break; - case "set_local": + case "local.set": addLocalInstruction( false, getInt( tokens, ++i), javaCodePos ); break; // case "get_global": diff --git a/test/de/inetsoftware/jwebassembly/module/WatParserTest.java b/test/de/inetsoftware/jwebassembly/module/WatParserTest.java index 371c370..bff521e 100644 --- a/test/de/inetsoftware/jwebassembly/module/WatParserTest.java +++ b/test/de/inetsoftware/jwebassembly/module/WatParserTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 Volker Berlin (i-net software) + * 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. @@ -88,13 +88,13 @@ public class WatParserTest { } @Test - public void getLocal() throws IOException { - test( "get_local 1" ); + public void Local_get() throws IOException { + test( "local.get 1" ); } @Test - public void setLocal() throws IOException { - test( "set_local 2" ); + public void Local_set() throws IOException { + test( "local.set 2" ); } @Test diff --git a/test/de/inetsoftware/jwebassembly/runtime/CallFunctions.java b/test/de/inetsoftware/jwebassembly/runtime/CallFunctions.java index cbb8df2..63da35a 100644 --- a/test/de/inetsoftware/jwebassembly/runtime/CallFunctions.java +++ b/test/de/inetsoftware/jwebassembly/runtime/CallFunctions.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 - 2018 Volker Berlin (i-net software) + * Copyright 2017 - 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. @@ -82,8 +82,8 @@ public class CallFunctions extends AbstractBaseTest { return nativeMax( 4.5F,5.5F); } - @WasmTextCode( "get_local 0 " // - + "get_local 1 " // + @WasmTextCode( "local.get 0 " // + + "local.get 1 " // + "f32.max " // + "return" ) private static float nativeMax( float a, float b) { diff --git a/test/de/inetsoftware/jwebassembly/samples/FunctionParameters.wat b/test/de/inetsoftware/jwebassembly/samples/FunctionParameters.wat index 3b7fc49..fb93c62 100644 --- a/test/de/inetsoftware/jwebassembly/samples/FunctionParameters.wat +++ b/test/de/inetsoftware/jwebassembly/samples/FunctionParameters.wat @@ -1,10 +1,10 @@ (module (export "abc" (func $de/inetsoftware/jwebassembly/samples/FunctionParameters.singleInt)) (func $de/inetsoftware/jwebassembly/samples/FunctionParameters.singleInt (param i32) (local i32) - get_local 0 + local.get 0 i32.const 1 i32.add - set_local 1 + local.set 1 return ) ) \ No newline at end of file