Skip to content

Commit 9b554b1

Browse files
VolkerVolker
authored andcommitted
calculate the block type of a if block for the conditional operator.
1 parent 4e4fa8a commit 9b554b1

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

src/de/inetsoftware/jwebassembly/module/BranchManger.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
*/
1717
package de.inetsoftware.jwebassembly.module;
1818

19+
import java.util.ArrayDeque;
1920
import java.util.ArrayList;
2021
import java.util.Arrays;
2122
import java.util.Collections;
2223
import java.util.HashMap;
2324
import java.util.List;
2425

26+
import javax.annotation.Nonnull;
27+
2528
import de.inetsoftware.classparser.CodeInputStream;
2629
import de.inetsoftware.jwebassembly.WasmException;
2730

@@ -252,11 +255,12 @@ private void calculateIf( BranchNode parent, IfParsedBlock startBlock, List<Pars
252255
ParsedBlock parsedBlock = parsedOperations.get( i );
253256
if( parsedBlock.startPosition == gotoPos && parsedBlock.op == JavaBlockOperator.GOTO && parsedBlock.startPosition < parsedBlock.endPosition) {
254257
parsedOperations.remove( i );
255-
branch = new BranchNode( startPos, startBlock.endPosition, WasmBlockOperator.IF, null, ValueType.empty );
258+
branch = new BranchNode( startPos, startBlock.endPosition, WasmBlockOperator.IF, null );
256259
parent.add( branch );
257260
if( i > 0 ) {
258261
calculate( branch, parsedOperations.subList( 0, i ) );
259262
}
263+
branch.data = calculateBlockType( startPos, branch.endPos );
260264
endPos = parsedBlock.endPosition;
261265

262266
int breakDeep = calculateBreakDeep( parent, endPos );
@@ -313,6 +317,39 @@ private int calculateBreakDeep( BranchNode parent, int endPos ) {
313317
return deep;
314318
}
315319

320+
/**
321+
* Calculate the block type. The value type that is on the stack after the block.
322+
*
323+
* @param startPos
324+
* the start position of the block
325+
* @param endPos
326+
* the end position of the block
327+
* @return the value type
328+
*/
329+
@Nonnull
330+
private ValueType calculateBlockType( int startPos, int endPos ) {
331+
ArrayDeque<ValueType> stack = new ArrayDeque<>();
332+
stack.push( ValueType.empty );
333+
for( WasmInstruction instr : instructions ) {
334+
int codePos = instr.getCodePosition();
335+
if( codePos < startPos ) {
336+
continue;
337+
}
338+
if( codePos >= endPos ) {
339+
break;
340+
}
341+
int popCount = instr.getPopCount();
342+
for( int p = 0; p < popCount; p++ ) {
343+
stack.pop();
344+
}
345+
ValueType pushValue = instr.getPushValueType();
346+
if( pushValue != null ) {
347+
stack.push( pushValue );
348+
}
349+
}
350+
return stack.pop();
351+
}
352+
316353
/**
317354
* Calculate the blocks of a switch.
318355
*

0 commit comments

Comments
 (0)