diff --git a/src/de/inetsoftware/jwebassembly/module/BranchManger.java b/src/de/inetsoftware/jwebassembly/module/BranchManger.java index 0aba3aa..4d7fd7c 100644 --- a/src/de/inetsoftware/jwebassembly/module/BranchManger.java +++ b/src/de/inetsoftware/jwebassembly/module/BranchManger.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. @@ -28,6 +28,7 @@ import javax.annotation.Nonnull; import de.inetsoftware.classparser.CodeInputStream; import de.inetsoftware.classparser.TryCatchFinally; import de.inetsoftware.jwebassembly.WasmException; +import de.inetsoftware.jwebassembly.wasm.AnyType; import de.inetsoftware.jwebassembly.wasm.NumericOperator; import de.inetsoftware.jwebassembly.wasm.ValueType; import de.inetsoftware.jwebassembly.wasm.WasmBlockOperator; @@ -270,7 +271,7 @@ class BranchManger { if( i > 0 ) { calculate( branch, parsedOperations.subList( 0, i ) ); } - ValueType blockType = calculateBlockType( startPos, branch.endPos ); + AnyType blockType = calculateBlockType( startPos, branch.endPos ); branch.data = blockType; // end position can not be outside of the parent endPos = Math.min( parsedBlock.endPosition, parent.endPos ); @@ -340,8 +341,8 @@ class BranchManger { * @return the value type */ @Nonnull - private ValueType calculateBlockType( int startPos, int endPos ) { - ArrayDeque stack = new ArrayDeque<>(); + private AnyType calculateBlockType( int startPos, int endPos ) { + ArrayDeque stack = new ArrayDeque<>(); stack.push( ValueType.empty ); for( WasmInstruction instr : instructions ) { int codePos = instr.getCodePosition(); @@ -355,7 +356,7 @@ class BranchManger { for( int p = 0; p < popCount; p++ ) { stack.pop(); } - ValueType pushValue = instr.getPushValueType(); + AnyType pushValue = instr.getPushValueType(); if( pushValue != null ) { stack.push( pushValue ); } diff --git a/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java index 5c8e175..d0c2ff8 100644 --- a/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java @@ -241,7 +241,8 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder { case 76: // astore_1 case 77: // astore_2 case 78: // astore_3 - addLoadStoreInstruction( ValueType.anyref, false, op - 75, codePos ); + storeType = findPreviousPushInstructionPushValueType(); + addLoadStoreInstruction( storeType, false, op - 75, codePos ); break; case 79: // iastore addArrayInstruction( ArrayOperator.SET, ValueType.i32, codePos ); @@ -729,7 +730,7 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder { List instructions = getInstructions(); for( int i = instructions.size() - 1; i >= 0; i-- ) { WasmInstruction instr = instructions.get( i ); - ValueType valueType = instr.getPushValueType(); + AnyType valueType = instr.getPushValueType(); if( valueType != null ) { valueCount++; } @@ -747,12 +748,12 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder { * @return the type of the last push value */ @Nonnull - private ValueType findPreviousPushInstructionPushValueType() { + private AnyType findPreviousPushInstructionPushValueType() { int valueCount = 0; List instructions = getInstructions(); for( int i = instructions.size() - 1; i >= 0; i-- ) { WasmInstruction instr = instructions.get( i ); - ValueType valueType = instr.getPushValueType(); + AnyType valueType = instr.getPushValueType(); if( valueType != null ) { if( ++valueCount == 1 ) { return valueType; diff --git a/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java b/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java index 42de449..fb3d0a8 100644 --- a/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java +++ b/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.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. @@ -21,6 +21,7 @@ import java.util.Arrays; import java.util.List; import de.inetsoftware.jwebassembly.WasmException; +import de.inetsoftware.jwebassembly.wasm.AnyType; import de.inetsoftware.jwebassembly.wasm.ValueType; /** @@ -39,7 +40,7 @@ class LocaleVariableManager { private boolean needTempI32; - private ArrayList localTypes = new ArrayList<>(); + private ArrayList localTypes = new ArrayList<>(); /** * Create a new instance. @@ -73,7 +74,7 @@ class LocaleVariableManager { * @param slot * the memory/slot index of the local variable */ - void use( ValueType valueType, int slot ) { + void use( AnyType valueType, int slot ) { if( slot < 0 ) { needTempI32 = true; return; @@ -112,7 +113,7 @@ class LocaleVariableManager { * the count of method parameter which should be exclude * @return the reused list with fresh values */ - List getLocalTypes( int paramCount ) { + List getLocalTypes( int paramCount ) { localTypes.clear(); for( int i = 0; i < size; i++ ) { LocaleVariable var = variables[i]; @@ -154,7 +155,7 @@ class LocaleVariableManager { * the memory/slot index of the local variable in Java * @return the ValueType */ - ValueType getValueType( int slot ) { + AnyType getValueType( int slot ) { return variables[slot].valueType; } @@ -178,8 +179,8 @@ class LocaleVariableManager { * The state of a single local variable slot. */ private static class LocaleVariable { - private ValueType valueType; + private AnyType valueType; - private int idx = -1; + private int idx = -1; } } diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index dd3989c..c467d9c 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -386,7 +386,7 @@ public class ModuleGenerator { if( !isStatic ) { paramCount++; } - List localTypes = codeBuilder.getLocalTypes( paramCount ); + List localTypes = codeBuilder.getLocalTypes( paramCount ); for( int i = 0; i < localTypes.size(); i++ ) { type = localTypes.get( i ); String paramName = null; diff --git a/src/de/inetsoftware/jwebassembly/module/WasmArrayInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmArrayInstruction.java index 7f1fe0c..75b0a40 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmArrayInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmArrayInstruction.java @@ -72,7 +72,7 @@ class WasmArrayInstruction extends WasmInstruction { /** * {@inheritDoc} */ - ValueType getPushValueType() { + AnyType getPushValueType() { switch( op ) { case NEW: return ValueType.anyref; @@ -95,10 +95,11 @@ class WasmArrayInstruction extends WasmInstruction { switch( op ) { case NEW: case GET: + return 2; case LENGTH: return 1; case SET: - return 0; + return 3; default: throw new WasmException( "Unknown array operation: " + op, -1 ); } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmBlockInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmBlockInstruction.java index d834844..8547485 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmBlockInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmBlockInstruction.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. @@ -22,6 +22,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import de.inetsoftware.jwebassembly.module.WasmInstruction.Type; +import de.inetsoftware.jwebassembly.wasm.AnyType; import de.inetsoftware.jwebassembly.wasm.ValueType; import de.inetsoftware.jwebassembly.wasm.WasmBlockOperator; @@ -71,7 +72,7 @@ class WasmBlockInstruction extends WasmInstruction { /** * {@inheritDoc} */ - ValueType getPushValueType() { + AnyType getPushValueType() { switch( op ) { case IF: return data != ValueType.empty ? (ValueType)data : null; diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java index 5892139..c228d8a 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java @@ -79,7 +79,7 @@ class WasmCallInstruction extends WasmInstruction { /** * {@inheritDoc} */ - ValueType getPushValueType() { + AnyType getPushValueType() { countParams(); return valueType; } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java index cefe5ce..0072392 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java @@ -57,7 +57,7 @@ public abstract class WasmCodeBuilder { * the count of method parameter which should be exclude * @return the reused list with fresh values */ - List getLocalTypes( int paramCount ) { + List getLocalTypes( int paramCount ) { return localVariables.getLocalTypes( paramCount ); } @@ -89,7 +89,7 @@ public abstract class WasmCodeBuilder { * the code position/offset in the Java method */ @Nonnull - protected void addLoadStoreInstruction( ValueType valueType, boolean load, @Nonnegative int javaIdx, int javaCodePos ) { + protected void addLoadStoreInstruction( AnyType valueType, boolean load, @Nonnegative int javaIdx, int javaCodePos ) { localVariables.use( valueType, javaIdx ); instructions.add( new WasmLoadStoreInstruction( load, javaIdx, localVariables, javaCodePos ) ); } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java index 1a80802..f831ada 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmConstInstruction.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. @@ -22,6 +22,7 @@ import javax.annotation.Nonnull; import de.inetsoftware.jwebassembly.WasmException; import de.inetsoftware.jwebassembly.module.WasmInstruction.Type; +import de.inetsoftware.jwebassembly.wasm.AnyType; import de.inetsoftware.jwebassembly.wasm.ValueType; /** @@ -106,7 +107,7 @@ class WasmConstInstruction extends WasmInstruction { /** * {@inheritDoc} */ - ValueType getPushValueType() { + AnyType getPushValueType() { return valueType; } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmConvertInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmConvertInstruction.java index ba2549c..5b0f516 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmConvertInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmConvertInstruction.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. @@ -18,6 +18,7 @@ package de.inetsoftware.jwebassembly.module; import java.io.IOException; import de.inetsoftware.jwebassembly.module.WasmInstruction.Type; +import de.inetsoftware.jwebassembly.wasm.AnyType; import de.inetsoftware.jwebassembly.wasm.ValueType; /** @@ -62,7 +63,7 @@ class WasmConvertInstruction extends WasmInstruction { /** * {@inheritDoc} */ - ValueType getPushValueType() { + AnyType getPushValueType() { switch( conversion ) { case l2i: case f2i: diff --git a/src/de/inetsoftware/jwebassembly/module/WasmGlobalInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmGlobalInstruction.java index dc849b4..321cf53 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmGlobalInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmGlobalInstruction.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. @@ -22,6 +22,7 @@ import javax.annotation.Nonnull; import de.inetsoftware.classparser.Member; import de.inetsoftware.jwebassembly.module.WasmInstruction.Type; +import de.inetsoftware.jwebassembly.wasm.AnyType; import de.inetsoftware.jwebassembly.wasm.ValueType; /** @@ -71,7 +72,7 @@ class WasmGlobalInstruction extends WasmInstruction { /** * {@inheritDoc} */ - ValueType getPushValueType() { + AnyType getPushValueType() { return load ? ValueType.getValueType( ref.getType() ) : null; } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java index 2eccaa8..302ee48 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmInstruction.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. @@ -21,7 +21,7 @@ import java.io.IOException; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import de.inetsoftware.jwebassembly.wasm.ValueType; +import de.inetsoftware.jwebassembly.wasm.AnyType; /** * Base class of all WasmInstruction. @@ -89,7 +89,7 @@ abstract class WasmInstruction { * @return the ValueType or null if no value is push */ @Nullable - abstract ValueType getPushValueType(); + abstract AnyType getPushValueType(); /** * Get the count of values that are removed from the stack. diff --git a/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java index 519b776..e33562f 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmLoadStoreInstruction.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. @@ -22,7 +22,7 @@ import javax.annotation.Nonnegative; import javax.annotation.Nonnull; import de.inetsoftware.jwebassembly.module.WasmInstruction.Type; -import de.inetsoftware.jwebassembly.wasm.ValueType; +import de.inetsoftware.jwebassembly.wasm.AnyType; /** * WasmInstruction for load and store local variables. @@ -80,7 +80,7 @@ class WasmLoadStoreInstruction extends WasmInstruction { /** * {@inheritDoc} */ - ValueType getPushValueType() { + AnyType getPushValueType() { return load ? localVariables.getValueType( idx ) : null; } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmLocalInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmLocalInstruction.java index b3446df..658df2f 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmLocalInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmLocalInstruction.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. @@ -22,7 +22,7 @@ import javax.annotation.Nonnegative; import javax.annotation.Nonnull; import de.inetsoftware.jwebassembly.module.WasmInstruction.Type; -import de.inetsoftware.jwebassembly.wasm.ValueType; +import de.inetsoftware.jwebassembly.wasm.AnyType; /** * WasmInstruction for load and store local variables. @@ -74,7 +74,7 @@ class WasmLocalInstruction extends WasmInstruction { /** * {@inheritDoc} */ - ValueType getPushValueType() { + AnyType getPushValueType() { return null; } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmNopInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmNopInstruction.java index 78895a6..da74c6e 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmNopInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmNopInstruction.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. @@ -21,7 +21,7 @@ import java.io.IOException; import javax.annotation.Nonnull; import de.inetsoftware.jwebassembly.module.WasmInstruction.Type; -import de.inetsoftware.jwebassembly.wasm.ValueType; +import de.inetsoftware.jwebassembly.wasm.AnyType; /** * WasmInstruction for nop. @@ -59,7 +59,7 @@ class WasmNopInstruction extends WasmInstruction { /** * {@inheritDoc} */ - ValueType getPushValueType() { + AnyType getPushValueType() { return null; } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmNumericInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmNumericInstruction.java index b3b0d0e..9cbff71 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmNumericInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmNumericInstruction.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. @@ -22,6 +22,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import de.inetsoftware.jwebassembly.module.WasmInstruction.Type; +import de.inetsoftware.jwebassembly.wasm.AnyType; import de.inetsoftware.jwebassembly.wasm.NumericOperator; import de.inetsoftware.jwebassembly.wasm.ValueType; @@ -71,7 +72,7 @@ class WasmNumericInstruction extends WasmInstruction { /** * {@inheritDoc} */ - ValueType getPushValueType() { + AnyType getPushValueType() { switch( numOp ) { case eq: case ne: diff --git a/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java index e874ff9..4b90f77 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java @@ -39,7 +39,7 @@ class WasmStructInstruction extends WasmInstruction { private final String typeName; - private AnyType type; + private AnyType type; private final String fieldName; @@ -99,14 +99,14 @@ class WasmStructInstruction extends WasmInstruction { /** * {@inheritDoc} */ - ValueType getPushValueType() { + AnyType getPushValueType() { switch( op ) { case NEW: case NEW_DEFAULT: case NULL: return ValueType.anyref; case GET: - return type instanceof ValueType ? (ValueType)type : ValueType.anyref; + return type; case SET: return null; default: @@ -125,6 +125,7 @@ class WasmStructInstruction extends WasmInstruction { case GET: return 1; case SET: + case NULL: return 0; default: throw new WasmException( "Unknown array operation: " + op, -1 ); diff --git a/src/de/inetsoftware/jwebassembly/wasm/ValueTypeParser.java b/src/de/inetsoftware/jwebassembly/wasm/ValueTypeParser.java index e32c1d0..202e776 100644 --- a/src/de/inetsoftware/jwebassembly/wasm/ValueTypeParser.java +++ b/src/de/inetsoftware/jwebassembly/wasm/ValueTypeParser.java @@ -99,7 +99,7 @@ public class ValueTypeParser implements Iterator { case 'J': // long return ValueType.i64; case 'V': // void - return ValueType.empty; + return null; default: throw new WasmException( "Not supported Java data type in method signature: " + sig.substring( idx - 1 ), -1 ); }