Skip to content

Commit

Permalink
use for IF conditions also a block with input parameter to prevent pr…
Browse files Browse the repository at this point in the history
…oblems with not splitable instruction like DUP (Java) or TEE (Wasm) #43
  • Loading branch information
Horcrux7 committed Jun 19, 2022
1 parent 2b27364 commit 47c28f7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 44 deletions.
14 changes: 5 additions & 9 deletions src/de/inetsoftware/jwebassembly/module/BranchManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -531,39 +531,35 @@ private boolean addBreakIfLoopContinue( BranchNode parent, ParsedBlock startBloc
private void calculateIf( BranchNode parent, IfParsedBlock startBlock, List<ParsedBlock> parsedOperations ) {
IfPositions positions = searchElsePosition( startBlock, parsedOperations );

BlockType conditionType = getConditionType();
BranchNode branch;
int endPos = positions.elsePos;
int startPos = startBlock.nextPosition - 1;
boolean createThenBlock = endPos <= parent.endPos;
if( createThenBlock ) {
// normal IF block
int startPos = startBlock.nextPosition;

if( startPos > endPos ) {
// the condition in a do while(condition) loop
int breakDeep = calculateContinueDeep( parent, endPos );
instructions.add( findIdxOfCodePos( startPos ), new WasmBlockInstruction( WasmBlockOperator.BR_IF, breakDeep, startPos - 1, startBlock.lineNumber ) );
instructions.add( findIdxOfCodePos( startPos + 1 ), new WasmBlockInstruction( WasmBlockOperator.BR_IF, breakDeep, startPos, startBlock.lineNumber ) );
return;
}

// find the code position where the condition values are push on the stack
List<WasmInstruction> instructions = this.instructions;
int idx = instructions.indexOf( startBlock.instr );
startPos = WasmCodeBuilder.findBlockStart( 1, instructions, idx + 1 );
if( parent.overlapped( startPos ) ) {
branch = addMiddleNode( parent, parent.startPos, endPos );
} else {
branch = new BranchNode( startPos, endPos, WasmBlockOperator.BLOCK, WasmBlockOperator.END );
branch = new BranchNode( startPos, endPos, WasmBlockOperator.BLOCK, WasmBlockOperator.END, conditionType );
parent.add( branch );
}
} else {
branch = parent;
// a jump outside of the parent, we will create the block later
}
breakOperations.add( new BreakBlock( WasmBlockOperator.BR_IF, branch, startBlock.nextPosition - 1, startBlock.endPosition ) );
breakOperations.add( new BreakBlock( WasmBlockOperator.BR_IF, branch, startPos, startBlock.endPosition ) );

for( int i = 0; i < positions.ifCount; i++ ) {
IfParsedBlock parsedBlock = (IfParsedBlock)parsedOperations.remove( 0 );
//instructions.remove( parsedBlock.jump );
breakOperations.add( new BreakBlock( WasmBlockOperator.BR_IF, branch, parsedBlock.nextPosition - 1, parsedBlock.endPosition ) );
}

Expand Down
36 changes: 1 addition & 35 deletions src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package de.inetsoftware.jwebassembly.module;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
Expand Down Expand Up @@ -156,39 +155,6 @@ boolean isEndsWithReturn() {
return false;
}

/**
* We need a value (or several) from the stack inside a block. We need to find the WasmInstruction where the block
* can can begin. If it is a function call or a numeric expression, it can be complicated to find the right point.
*
* @param count
* the count of values on the stack back. 1 means the last value. 2 means the penultimate value.
* @param instructions
* the instruction list for searching
* @param idx
* the start index for the search. Between 0 and instructions.size().
* @return the code position that push the last instruction
*/
static int findBlockStart( int count, List<WasmInstruction> instructions, int idx ) {
int valueCount = 0;
for( int i = idx - 1; i >= 0; i-- ) {
WasmInstruction instr = instructions.get( i );
AnyType valueType = instr.getPushValueType();
if( valueType != null ) {
valueCount++;
}
int popCount = instr.getPopCount();
valueCount -= popCount;
if( valueCount == count && popCount == 0 ) {
int codePos = instr.getCodePosition();
if( i == 0 || instructions.get( i - 1 ).getCodePosition() < codePos ) {
// if the same codePos is used from multiple instructions then it is not an atomic operation in Java
return codePos;
}
}
}
throw new WasmException( "Block start position not found", -1 ); // should never occur
}

/**
* We need the value type from the stack.
*
Expand All @@ -210,7 +176,7 @@ AnyType findValueTypeFromStack( int count, int javaCodePos ) {
* the count of values on the stack back. 1 means the last value. 2 means the penultimate value.
* @param javaCodePos
* current code position for which the stack is inspected
* @return
* @return the type
*/
@Nonnull
AnyType findArrayTypeFromStack( int count, int javaCodePos ) {
Expand Down

0 comments on commit 47c28f7

Please sign in to comment.