Add support for a block with input and return type.

This commit is contained in:
Volker Berlin 2022-06-18 19:47:43 +02:00
parent 4bb7f8f555
commit 511d31af67
No known key found for this signature in database
GPG Key ID: 988423EF815BE4CB

View File

@ -65,7 +65,7 @@ class BranchManager {
private final ArrayList<BreakBlock> breakOperations = new ArrayList<>(); private final ArrayList<BreakBlock> breakOperations = new ArrayList<>();
private BlockType switchType; private BlockType conditionType;
/** /**
* Create a branch manager. * Create a branch manager.
@ -777,23 +777,17 @@ class BranchManager {
} }
/** /**
* Calculate the deep of a break. A GOTO or IF in Java can jump out multiple loops. We need to calculate how many levels we need to jump. * Get the block type for condition like a SWITCH or IF. This is a block with a i32 as input parameter.
* *
* @param parent * @return the block type
* the current parent node.
* @param endPos
* the end position of the jump
* @return the deep level for "br" or -1 if there is no parent node with this end position
*/ */
private int calculateBreakDeep( BranchNode parent, int endPos ) { @Nonnull
int deep = -1; private BlockType getConditionType() {
boolean wasLoop = false; BlockType conditionType = this.conditionType;
while( parent != null && parent.elseEndPos <= endPos && parent.data == null ) { if( conditionType == null ) {
deep++; this.conditionType = conditionType = options.types.blockType( Collections.singletonList( ValueType.i32 ), Collections.emptyList() );
wasLoop |= parent.startOp == WasmBlockOperator.LOOP; // only in a loop we need to jump for exit
parent = parent.parent;
} }
return wasLoop ? deep : -1; return conditionType;
} }
/** /**
@ -867,10 +861,7 @@ class BranchManager {
* the not consumed operations in the parent branch * the not consumed operations in the parent branch
*/ */
private void calculateSwitch( BranchNode parent, SwitchParsedBlock switchBlock, List<ParsedBlock> parsedOperations ) { private void calculateSwitch( BranchNode parent, SwitchParsedBlock switchBlock, List<ParsedBlock> parsedOperations ) {
BlockType switchType = this.switchType; BlockType switchType = getConditionType();
if( switchType == null ) {
this.switchType = switchType = options.types.blockType( Arrays.asList( ValueType.i32 ), Collections.emptyList() );
}
int startPosition = ((ParsedBlock)switchBlock).startPosition; int startPosition = ((ParsedBlock)switchBlock).startPosition;
int posCount = switchBlock.positions.length; int posCount = switchBlock.positions.length;
boolean isTable = switchBlock.keys == null; boolean isTable = switchBlock.keys == null;
@ -1636,7 +1627,7 @@ class BranchManager {
/** /**
* Described a code branch/block node in a tree structure. * Described a code branch/block node in a tree structure.
*/ */
private static class BranchNode extends ArrayList<BranchNode> { private class BranchNode extends ArrayList<BranchNode> {
private final int startPos; private final int startPos;
@ -1898,8 +1889,9 @@ class BranchManager {
AnyType result = stack.pop(); AnyType result = stack.pop();
if( blockType == null ) { if( blockType == null ) {
startBlock.setData( result ); startBlock.setData( result );
} else if( result != ValueType.empty) { } else if( result != ValueType.empty ) {
throw new WasmException( "block with parameter has return parameter", startBlock.getLineNumber() ); result = options.types.blockType( blockType.getParams(), Collections.singletonList( result ) );
startBlock.setData( result );
} }
} catch( Throwable th ) { } catch( Throwable th ) {
throw WasmException.create( th, startBlock.getLineNumber() ); throw WasmException.create( th, startBlock.getLineNumber() );