get_local --> local.get, set_local --> local.set, see #3

This commit is contained in:
Volker Berlin 2019-02-20 21:42:52 +01:00
parent de320797a6
commit f5ed8aeeb6
11 changed files with 75 additions and 61 deletions

View File

@ -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 );
}

View File

@ -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;

View File

@ -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

View File

@ -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

View File

@ -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;
}
}

View File

@ -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 ) );
}
/**

View File

@ -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,
}

View File

@ -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":

View File

@ -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

View File

@ -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) {

View File

@ -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
)
)