mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-25 07:27:52 +01:00
new branch manager with internal hierarchy.
This commit is contained in:
parent
e61ad1e1a0
commit
97283a1d39
@ -18,6 +18,7 @@ package de.inetsoftware.jwebassembly.module;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import de.inetsoftware.classparser.CodeInputStream;
|
import de.inetsoftware.classparser.CodeInputStream;
|
||||||
|
|
||||||
@ -29,13 +30,16 @@ import de.inetsoftware.classparser.CodeInputStream;
|
|||||||
*/
|
*/
|
||||||
class BranchManger {
|
class BranchManger {
|
||||||
|
|
||||||
private final ArrayList<Block> stack = new ArrayList<>();
|
private final ArrayList<ParsedBlock> allParsedOperations = new ArrayList<>();
|
||||||
|
|
||||||
|
private final BranchNode root = new BranchNode( 0, Integer.MAX_VALUE, null, null );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove all branch information.
|
* Remove all branch information for reusing the manager.
|
||||||
*/
|
*/
|
||||||
void reset() {
|
void reset() {
|
||||||
stack.clear();
|
allParsedOperations.clear();
|
||||||
|
root.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -49,19 +53,31 @@ class BranchManger {
|
|||||||
* the relative jump position
|
* the relative jump position
|
||||||
*/
|
*/
|
||||||
void start( BlockOperator op, int startPosition, int offset ) {
|
void start( BlockOperator op, int startPosition, int offset ) {
|
||||||
stack.add( new Block( op, startPosition, offset ) );
|
allParsedOperations.add( new ParsedBlock( op, startPosition, offset ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate all block operators from the parsed information.
|
* Calculate all block operators from the parsed information.
|
||||||
*/
|
*/
|
||||||
void calculate() {
|
void calculate() {
|
||||||
for( int i = 0; i < stack.size(); i++ ) {
|
calculate( root, allParsedOperations );
|
||||||
Block block = stack.get( i );
|
}
|
||||||
switch( block.op ) {
|
|
||||||
|
/**
|
||||||
|
* Calculate the branch tree for the given branch and parsed sub operations.
|
||||||
|
*
|
||||||
|
* @param parent the parent branch/block in the hierarchy.
|
||||||
|
* @param parsedOperations the not consumed parsed operations of the parent block.
|
||||||
|
*/
|
||||||
|
private void calculate( BranchNode parent, List<ParsedBlock> parsedOperations ) {
|
||||||
|
while( !parsedOperations.isEmpty() ) {
|
||||||
|
ParsedBlock parsedBlock = parsedOperations.remove( 0 );
|
||||||
|
switch( parsedBlock.op ) {
|
||||||
case IF:
|
case IF:
|
||||||
caculateIf( i, block );
|
caculateIf( parent, parsedBlock, parsedOperations );
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalStateException( "Unimplemented block code operation: " + parsedBlock.op );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -69,39 +85,54 @@ class BranchManger {
|
|||||||
/**
|
/**
|
||||||
* Calculate the ELSE and END position of an IF control structure.
|
* Calculate the ELSE and END position of an IF control structure.
|
||||||
*
|
*
|
||||||
* @param i
|
* @param parent
|
||||||
* the index in the stack
|
* the parent branch
|
||||||
* @param startBlock
|
* @param startBlock
|
||||||
* the start block of the if control structure
|
* the start block of the if control structure
|
||||||
|
* @param parsedOperations
|
||||||
|
* the not consumed operations in the parent branch
|
||||||
*/
|
*/
|
||||||
private void caculateIf( int i, Block startBlock ) {
|
private void caculateIf( BranchNode parent, ParsedBlock startBlock, List<ParsedBlock> parsedOperations ) {
|
||||||
i++;
|
int i = 0;
|
||||||
int gotoPos = startBlock.endPosition - 3; // 3 - byte size of goto instruction
|
int endPos = Math.min( startBlock.endPosition, parent.endPos );
|
||||||
for( ; i < stack.size(); i++ ) {
|
int gotoPos = endPos - 3; // 3 - byte size of goto instruction
|
||||||
Block block = stack.get( i );
|
BranchNode branch = null;
|
||||||
if( block.startPosition == gotoPos && block.op == BlockOperator.GOTO ) {
|
for( ; i < parsedOperations.size(); i++ ) {
|
||||||
block.op = BlockOperator.ELSE;
|
ParsedBlock parsedBlock = parsedOperations.get( i );
|
||||||
block.startPosition += 3;
|
if( parsedBlock.startPosition == gotoPos && parsedBlock.op == BlockOperator.GOTO ) {
|
||||||
startBlock = block;
|
parsedOperations.remove( i );
|
||||||
i++;
|
branch = new BranchNode( startBlock.startPosition, startBlock.endPosition, BlockOperator.IF, null );
|
||||||
|
parent.add( branch );
|
||||||
|
if( i > 0 ) {
|
||||||
|
calculate( branch, parsedOperations.subList( 0, i ) );
|
||||||
|
}
|
||||||
|
endPos = parsedBlock.endPosition;
|
||||||
|
branch = new BranchNode( startBlock.endPosition, endPos, BlockOperator.ELSE, BlockOperator.END );
|
||||||
|
parent.add( branch );
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if( block.startPosition > gotoPos ) {
|
if( parsedBlock.startPosition > gotoPos ) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if( branch == null ) {
|
||||||
|
branch = new BranchNode( startBlock.startPosition, endPos, BlockOperator.IF, BlockOperator.END );
|
||||||
|
parent.add( branch );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Search the index in the stack to add the END operator
|
* Search the index in the stack to add the END operator
|
||||||
*/
|
*/
|
||||||
int endPos = startBlock.endPosition;
|
for( ; i < parsedOperations.size(); i++ ) {
|
||||||
for( ; i < stack.size(); i++ ) {
|
ParsedBlock parsedBlock = parsedOperations.get( i );
|
||||||
Block block = stack.get( i );
|
if( parsedBlock.startPosition >= endPos ) {
|
||||||
if( block.startPosition >= endPos ) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stack.add( i, new Block( BlockOperator.END, endPos, 0 ) );
|
if( i > 0 ) {
|
||||||
|
calculate( branch, parsedOperations.subList( 0, i ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -115,31 +146,71 @@ class BranchManger {
|
|||||||
* if any I/O exception occur
|
* if any I/O exception occur
|
||||||
*/
|
*/
|
||||||
void handle( CodeInputStream byteCode, ModuleWriter writer ) throws IOException {
|
void handle( CodeInputStream byteCode, ModuleWriter writer ) throws IOException {
|
||||||
if( stack.isEmpty() ) {
|
root.handle( byteCode.getCodePosition(), writer );
|
||||||
return;
|
|
||||||
}
|
|
||||||
int position = byteCode.getCodePosition();
|
|
||||||
Block block = stack.get( 0 );
|
|
||||||
if( block.startPosition == position ) {
|
|
||||||
writer.writeBlockCode( block.op );
|
|
||||||
stack.remove( 0 );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Description of single block/branch
|
* Description of single block/branch from the parsed Java byte code. The parsed branches are plain.
|
||||||
*/
|
*/
|
||||||
private static class Block {
|
private static class ParsedBlock {
|
||||||
private BlockOperator op;
|
private BlockOperator op;
|
||||||
|
|
||||||
private int startPosition;
|
private int startPosition;
|
||||||
|
|
||||||
private int endPosition;
|
private int endPosition;
|
||||||
|
|
||||||
private Block( BlockOperator op, int startPosition, int offset ) {
|
private ParsedBlock( BlockOperator op, int startPosition, int offset ) {
|
||||||
this.op = op;
|
this.op = op;
|
||||||
this.startPosition = startPosition;
|
this.startPosition = startPosition;
|
||||||
this.endPosition = startPosition + offset;
|
this.endPosition = startPosition + offset;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Described a code branch/block node in a tree structure.
|
||||||
|
*/
|
||||||
|
private static class BranchNode extends ArrayList<BranchNode> {
|
||||||
|
|
||||||
|
final int startPos;
|
||||||
|
|
||||||
|
final int endPos;
|
||||||
|
|
||||||
|
private final BlockOperator startOp;
|
||||||
|
|
||||||
|
private final BlockOperator endOp;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new description.
|
||||||
|
*
|
||||||
|
* @param startPos
|
||||||
|
* the start position in the Java code. Limit also the children.
|
||||||
|
* @param endPos
|
||||||
|
* the end position in the Java code. Limit also the children.
|
||||||
|
* @param startOp
|
||||||
|
* The WASM operation on the start position. Can be null if there is nothing in WASM.
|
||||||
|
* @param endOp
|
||||||
|
* the WASM operation on the end position. Can be null if there is nothing in WASM.
|
||||||
|
*/
|
||||||
|
BranchNode( int startPos, int endPos, BlockOperator startOp, BlockOperator endOp ) {
|
||||||
|
this.startPos = startPos;
|
||||||
|
this.endPos = endPos;
|
||||||
|
this.startOp = startOp;
|
||||||
|
this.endOp = endOp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void handle( int codePositions, ModuleWriter writer ) throws IOException {
|
||||||
|
if( codePositions < startPos || codePositions > endPos ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if( codePositions == startPos && startOp != null ) {
|
||||||
|
writer.writeBlockCode( startOp );
|
||||||
|
}
|
||||||
|
for( BranchNode branch : this ) {
|
||||||
|
branch.handle( codePositions, writer );
|
||||||
|
}
|
||||||
|
if( codePositions == endPos && endOp != null ) {
|
||||||
|
writer.writeBlockCode( endOp );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,7 @@ public class ControlFlowOperators extends AbstractBaseTest {
|
|||||||
addParam( list, script, "ifeq" );
|
addParam( list, script, "ifeq" );
|
||||||
addParam( list, script, "ifne" );
|
addParam( list, script, "ifne" );
|
||||||
addParam( list, script, "iflt" );
|
addParam( list, script, "iflt" );
|
||||||
|
addParam( list, script, "ifMultiple" );
|
||||||
addParam( list, script, "forLoop" );
|
addParam( list, script, "forLoop" );
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
@ -80,6 +81,23 @@ public class ControlFlowOperators extends AbstractBaseTest {
|
|||||||
return condition;
|
return condition;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Export
|
||||||
|
static int ifMultiple() {
|
||||||
|
int condition = 3;
|
||||||
|
if( condition <= 0 ) {
|
||||||
|
if( condition < 0 ) {
|
||||||
|
condition = 13;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if( condition > 0 ) {
|
||||||
|
condition++;
|
||||||
|
} else {
|
||||||
|
condition--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return condition;
|
||||||
|
}
|
||||||
|
|
||||||
@Export
|
@Export
|
||||||
static int forLoop() {
|
static int forLoop() {
|
||||||
int a = 0;
|
int a = 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user