fix BranchNode order for some complex constructs. #43

This commit is contained in:
Volker Berlin 2022-05-29 10:47:50 +02:00
parent d182bace9a
commit f393802418
No known key found for this signature in database
GPG Key ID: 988423EF815BE4CB

View File

@ -554,7 +554,7 @@ class BranchManager {
} }
if( createThenBlock ) { if( createThenBlock ) {
// handle jumps outside the then but inside calling parent // handle jumps outside the then but inside calling parent and create the needed blocks in the hierarchy. Typical this is the ELSE block.
for( int i = 0; i < parsedOperations.size(); i++ ) { for( int i = 0; i < parsedOperations.size(); i++ ) {
ParsedBlock block = parsedOperations.get( i ); ParsedBlock block = parsedOperations.get( i );
if( block.startPosition >= endPos ) { if( block.startPosition >= endPos ) {
@ -566,17 +566,14 @@ class BranchManager {
BranchNode nodeParent = branch; BranchNode nodeParent = branch;
BranchNode node; BranchNode node;
do { do {
node = nodeParent; node = nodeParent;
nodeParent = nodeParent.parent; nodeParent = nodeParent.parent;
} } while( nodeParent.endPos < endPosition );
while( nodeParent.endPos < endPosition );
if( nodeParent.endPos == endPosition ) { if( nodeParent.endPos == endPosition ) {
// there is already a block end on this position
continue; continue;
} }
nodeParent.remove( node ); addMiddleNode( nodeParent, branch.startPos, endPosition );
BranchNode elseBranch = new BranchNode( branch.startPos, endPosition, WasmBlockOperator.BLOCK, WasmBlockOperator.END );
elseBranch.add( node );
nodeParent.add( elseBranch );
} }
} }
} }
@ -1446,6 +1443,15 @@ class BranchManager {
insertConstBeforePosition( 0, breakBlock.breakPos, -1 ); insertConstBeforePosition( 0, breakBlock.breakPos, -1 );
} }
BranchNode breakNode = new BranchNode( breakBlock.breakPos, breakBlock.breakPos, op, null, deep + 1 ); BranchNode breakNode = new BranchNode( breakBlock.breakPos, breakBlock.breakPos, op, null, deep + 1 );
// add the BranchNode on the right position
int childCount = branch.size();
for( int i = 0; i < childCount; i++ ) {
BranchNode child = branch.get( i );
if( child.endPos> breakBlock.breakPos ) {
branch.add( i, breakNode );
return;
}
}
branch.add( breakNode ); branch.add( breakNode );
} }
@ -1720,7 +1726,7 @@ class BranchManager {
public boolean add( BranchNode node ) { public boolean add( BranchNode node ) {
node.parent = this; node.parent = this;
assert node.startOp == null || (node.startPos >= startPos && node.endPos <= endPos): "Node outside parent: " + this + " + " + node; assert node.startOp == null || (node.startPos >= startPos && node.endPos <= endPos): "Node outside parent: " + this + " + " + node;
// assert !overlapped( node.startPos ) : "Node on wrong level: " + node + "; parent: " + this + "; last: " + get( size() - 1 ); assert node.startOp == null || !overlapped( node.startPos ) : "Node on wrong level: " + node + "; parent: " + this + "; last: " + get( size() - 1 );
return super.add( node ); return super.add( node );
} }