mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-15 10:44:47 +01:00
use AnyType instead ValueType in the VariableManager
This commit is contained in:
parent
a06a93dd37
commit
145a39079b
@ -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<ValueType> stack = new ArrayDeque<>();
|
||||
private AnyType calculateBlockType( int startPos, int endPos ) {
|
||||
ArrayDeque<AnyType> 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 );
|
||||
}
|
||||
|
@ -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<WasmInstruction> 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<WasmInstruction> 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;
|
||||
|
@ -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<ValueType> localTypes = new ArrayList<>();
|
||||
private ArrayList<AnyType> 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<ValueType> getLocalTypes( int paramCount ) {
|
||||
List<AnyType> 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;
|
||||
}
|
||||
}
|
||||
|
@ -386,7 +386,7 @@ public class ModuleGenerator {
|
||||
if( !isStatic ) {
|
||||
paramCount++;
|
||||
}
|
||||
List<ValueType> localTypes = codeBuilder.getLocalTypes( paramCount );
|
||||
List<AnyType> localTypes = codeBuilder.getLocalTypes( paramCount );
|
||||
for( int i = 0; i < localTypes.size(); i++ ) {
|
||||
type = localTypes.get( i );
|
||||
String paramName = null;
|
||||
|
@ -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 );
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -79,7 +79,7 @@ class WasmCallInstruction extends WasmInstruction {
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
ValueType getPushValueType() {
|
||||
AnyType getPushValueType() {
|
||||
countParams();
|
||||
return valueType;
|
||||
}
|
||||
|
@ -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<ValueType> getLocalTypes( int paramCount ) {
|
||||
List<AnyType> 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 ) );
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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:
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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.
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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:
|
||||
|
@ -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 );
|
||||
|
@ -99,7 +99,7 @@ public class ValueTypeParser implements Iterator<AnyType> {
|
||||
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 );
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user