Package com.aparapi.internal.instruction
Class BranchSet
java.lang.Object
com.aparapi.internal.instruction.BranchSet
public class BranchSet extends Object
Deals with the issue of recognizing that a sequence of bytecode branch instructions actually represent a single if/while with a logical expression.
A logical expressions such as
if (i>= 0 && i%2 == 0 && i<100){}
gets translated into a sequence of bytecode level branches and targets. Which might look like the following.
a: if ? e +
b: if ? d |+
c: if ? e ||+
d: if ? out |v|+
e: ... v v|
... |
out: _instruction v
We need an algorithm for recognizing the underlying logical expression.
Essentially, given a set of branches, get the longest sequential sequence including the input set which target each other or _target. Branches can legally branch to another in the valid set, or to the fall through of the last in the valid set or to _target
So an
if(COND){IF_INSTRUCTIONS}else{ELSE_INSTUCTIONS}...will be
branch[?? branch]*, instructions*,goto,instruction*,target
and if(COND){IF_INSTRUCTIONS}...will be :-
branch[?? branch]*,instruction*,target
The psuedo code code the algorithm looks like this:
int n=0;
while (exp.length >1){
if (exp[n].target == exp[n+1].target){ #rule 1
replace exp[n] and exp[n+1] with a single expression representing 'exp[n] || exp[n+1]'
n=0;
}else if (exp[n].target == exp[n+1].next){ #rule 2
replace exp[n] and exp[n+1] with a single expression representing '!(exp[n]) && exp[n+1]
n=0;
}else{ #rule 3
n++;
}
}
result = !exp[0];
- Author:
- gfrost
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BranchSet.CompoundLogicalExpressionNode
A node in the expression tree representing a simple logical expression.static class
BranchSet.LogicalExpressionNode
Base abstract class used to hold information used to construct node tree for logical expressions.static class
BranchSet.SimpleLogicalExpressionNode
A node in the expression tree representing a simple logical expression. -
Constructor Summary
Constructors Constructor Description BranchSet(InstructionSet.Branch _branch)
We construct a branch set with the 'last' branch. -
Method Summary
Modifier and Type Method Description List<InstructionSet.ConditionalBranch>
getBranches()
Instruction
getFallThrough()
InstructionSet.Branch
getFirst()
InstructionSet.Branch
getLast()
BranchSet.LogicalExpressionNode
getLogicalExpression()
Instruction
getTarget()
void
unhook()
-
Constructor Details
-
BranchSet
We construct a branch set with the 'last' branch. It is assumed that all nodes prior to_branch
are folded. This will walk backwards until it finds a non-branch or until it finds a branch that does not below to this set.- Parameters:
_branch
-
-
-
Method Details
-
getBranches
-
getFirst
-
getLast
-
unhook
public void unhook() -
getTarget
-
getFallThrough
-
getLogicalExpression
-