Package com.aparapi.internal.instruction
Class BranchSet
- java.lang.Object
-
- com.aparapi.internal.instruction.BranchSet
-
public class BranchSet extends java.lang.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
gets translated into a sequence of bytecode level branches and targets. Which might look like the following.if (i>= 0 && i%2 == 0 && i<100){}
We need an algorithm for recognizing the underlying logical expression.a: if ? e + b: if ? d |+ c: if ? e ||+ d: if ? out |v|+ e: ... v v| ... | out: _instruction v
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
andbranch[?? branch]*, instructions*,goto,instruction*,target
if(COND){IF_INSTRUCTIONS}...
will be :-
The psuedo code code the algorithm looks like this:branch[?? branch]*,instruction*,target
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];
-
-
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.
-
Field Summary
Fields Modifier and Type Field Description private Instruction
fallThrough
private InstructionSet.Branch
first
private InstructionSet.Branch
last
private BranchSet.LogicalExpressionNode
logicalExpressionNode
private java.util.List<InstructionSet.ConditionalBranch>
set
private Instruction
target
-
Constructor Summary
Constructors Constructor Description BranchSet(InstructionSet.Branch _branch)
We construct a branch set with the 'last' branch.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.List<InstructionSet.ConditionalBranch>
getBranches()
Instruction
getFallThrough()
InstructionSet.Branch
getFirst()
InstructionSet.Branch
getLast()
BranchSet.LogicalExpressionNode
getLogicalExpression()
Instruction
getTarget()
void
unhook()
-
-
-
Field Detail
-
set
private final java.util.List<InstructionSet.ConditionalBranch> set
-
fallThrough
private final Instruction fallThrough
-
target
private final Instruction target
-
last
private final InstructionSet.Branch last
-
first
private InstructionSet.Branch first
-
logicalExpressionNode
private BranchSet.LogicalExpressionNode logicalExpressionNode
-
-
Constructor Detail
-
BranchSet
public BranchSet(InstructionSet.Branch _branch)
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 Detail
-
getBranches
public java.util.List<InstructionSet.ConditionalBranch> getBranches()
-
getFirst
public InstructionSet.Branch getFirst()
-
getLast
public InstructionSet.Branch getLast()
-
unhook
public void unhook()
-
getTarget
public Instruction getTarget()
-
getFallThrough
public Instruction getFallThrough()
-
getLogicalExpression
public BranchSet.LogicalExpressionNode getLogicalExpression()
-
-