Package com.aparapi.internal.instruction
Class BranchSet
java.lang.Object
com.aparapi.internal.instruction.BranchSet
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 invalid input: '&'invalid input: '&' i%2 == 0 invalid input: '&'invalid input: '&' iinvalid input: '<'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]) invalid input: '&'invalid input: '&' exp[n+1]
n=0;
}else{ #rule 3
n++;
}
}
result = !exp[0];
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
A node in the expression tree representing a simple logical expression.static class
Base abstract class used to hold information used to construct node tree for logical expressions.static class
A node in the expression tree representing a simple logical expression. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Instruction
private InstructionSet.Branch
private final InstructionSet.Branch
private BranchSet.LogicalExpressionNode
private final List
<InstructionSet.ConditionalBranch> private final Instruction
-
Constructor Summary
ConstructorsConstructorDescriptionBranchSet
(InstructionSet.Branch _branch) We construct a branch set with the 'last' branch. -
Method Summary
-
Field Details
-
set
-
fallThrough
-
target
-
last
-
first
-
logicalExpressionNode
-
-
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
-