Class 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

    
          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];