Package soot

Examples of soot.Unit


  protected void handleMultipleReturns()
  {
 
    if(this.getTails().size() > 1)
    {
      Unit stop = new ExitStmt();
        List<Unit> predsOfstop = new ArrayList<Unit>();
 
       
      for(Iterator<Unit> tailItr = this.getTails().iterator(); tailItr.hasNext(); )
      {
        Unit tail = tailItr.next();
        predsOfstop.add(tail);
     
        List<Unit> tailSuccs = this.unitToSuccs.get(tail);
        tailSuccs.add(stop)
      }
View Full Code Here


   */
 
  protected void removeBogusHeads()
  {
    Chain<Unit> units = body.getUnits();
    Unit trueHead = units.getFirst();
   
    while(this.getHeads().size() > 1)
    {
      for(Iterator<Unit> headItr = this.getHeads().iterator(); headItr.hasNext(); )
      {
        Unit head = headItr.next();
        if(trueHead == head)
          continue;
       
        this.unitToPreds.remove(head);
        List<Unit> succs = this.unitToSuccs.get(head);
        for(Iterator<Unit> succsItr = succs.iterator(); succsItr.hasNext(); )
        {
          List<Unit> tobeRemoved = new ArrayList<Unit>();
         
          Unit succ = succsItr.next();
          List<Unit> predOfSuccs = this.unitToPreds.get(succ);
         

          for(Iterator<Unit> predItr = predOfSuccs.iterator(); predItr.hasNext(); )
          {
            Unit pred = predItr.next();
            if(pred == head)
              tobeRemoved.add(pred)
           
          }
         
View Full Code Here

    List<Unit> tails = this.getTails();

    TailsLoop:   
    for(Iterator<Unit> itr = tails.iterator(); itr.hasNext(); )
    {
      Unit tail = itr.next();
      if(!(tail instanceof ThrowStmt))
        continue;
     
      DominatorNode x = dom.getDode(tail);
      DominatorNode parentOfX = dom.getParentOf(x);
      Object xgode = x.getGode();
      DominatorNode xpdomDode = pdom.getDode(xgode);
      Object parentXGode = parentOfX.getGode();
      DominatorNode parentpdomDode = pdom.getDode(parentXGode);
      //while x post-dominates its dominator (parent in dom)
      while(pdom.isDominatorOf(xpdomDode, parentpdomDode))
      {
        x = parentOfX;
        parentOfX = dom.getParentOf(x);
       
        //If parent is null we must be at the head of the graph
        if(parentOfX == null)
          //throw new RuntimeException("This should never have happened!");
          break;
       
        xgode = x.getGode();
        xpdomDode = pdom.getDode(xgode);
        parentXGode = parentOfX.getGode();
        parentpdomDode = pdom.getDode(parentXGode);
      }
     
      if(parentOfX != null)
        x = parentOfX;
     
      xgode = x.getGode();
      xpdomDode = pdom.getDode(xgode);
     
     
      Unit mergePoint = null;
     
      if(x2mergePoint.containsKey(xgode))
        mergePoint = x2mergePoint.get(xgode);
      else
      {
        //Now get all the children of x in the dom
       
        List<DominatorNode> domChilds = dom.getChildrenOf(x);
               
        Object child1god = null;
        Object child2god = null;
       
        for(Iterator<DominatorNode> domItr = domChilds.iterator(); domItr.hasNext(); )
        {
          DominatorNode child = domItr.next();
          Object childGode = child.getGode();
          DominatorNode childpdomDode = pdom.getDode(childGode);
         
         
          //we don't want to make a loop!
          List<Unit> path = this.getExtendedBasicBlockPathBetween((Unit)childGode, tail);
         
          //if(dom.isDominatorOf(child, dom.getDode(tail)))
          if(!(path == null || path.size() == 0))
            continue;
         
          if(pdom.isDominatorOf(childpdomDode, xpdomDode))
          {
            mergePoint = (Unit) child.getGode();       
            break;
          }         
                 
          //gather two eligible childs
          if(child1god == null)
            child1god = childGode;
          else if(child2god == null)
            child2god = childGode;
           
        }
       
        if(mergePoint == null)
        {
          if(child1god != null && child2god != null)
          {
            DominatorNode child1 = pdom.getDode(child1god);
            DominatorNode child2 = pdom.getDode(child2god);
 
            //go up the pdom tree and find the common parent of child1 and child2
            DominatorNode comParent = child1.getParent();
            while(comParent != null)
            {
              if(pdom.isDominatorOf(comParent, child2))
              {
                mergePoint = (Unit) comParent.getGode();
                break;
              }
              comParent = comParent.getParent();
            }
          }
          else if(child1god != null || child2god != null){
         
            DominatorNode y = null;
           
            if(child1god != null)
              y = pdom.getDode(child1god);
            else if(child2god != null)
              y = pdom.getDode(child2god);
           
             
            DominatorNode initialY = dom.getDode(y.getGode());
            DominatorNode yDodeInDom = initialY;
           
            while(dom.isDominatorOf(x, yDodeInDom))
            {
              y = y.getParent();
             
              //If this is a case where the childs of a conditional
              //are all throws, or returns, just forget it!
              if(y == null)
              {
                break ;
              }
              yDodeInDom = dom.getDode(y.getGode());
            }
            if(y != null)
              mergePoint = (Unit) y.getGode();
            else
              mergePoint = (Unit) initialY.getGode();
          }
        }
       
        //This means no (dom) child of x post-dominates x, so just use the child that is
        //immediately
        /*if(mergePoint == null)
        {
          //throw new RuntimeException("No child post-dominates x.");
          mergePoint = potentialMergePoint;
         
        }*/
        //This means no (dom) child of x post-dominates x, so just use the child that is
        //immediately. this means there is no good reliable merge point. So we just fetch the succ
        //of x in CFg so that the succ does not dominate the throw, and find the first
        //post-dom of the succ so that x does not dom it.
        //
        if(mergePoint == null)
        {
          List<Unit> xSucc = this.unitToSuccs.get(x.getGode());
          for(Iterator<Unit> uItr = xSucc.iterator(); uItr.hasNext(); )
          {
            Unit u = uItr.next();
            if(dom.isDominatorOf(dom.getDode(u), dom.getDode(tail)))
              continue;
           
           
            DominatorNode y = pdom.getDode(u);
View Full Code Here

       
        /**
         * Find the real header of this handler block
         *
         */
        Unit handler = trap.getHandlerUnit();
       
        Unit pred = handler;
        while(this.unitToPreds.get(pred).size() > 0)
          pred = this.unitToPreds.get(pred).get(0);
       
       
        handler2header.put(handler, pred);
        /***********/
       
        /*
         * Keep this here for possible future changes.
         */
        /*GuardedBlock gb = new GuardedBlock(trap.getBeginUnit(), trap.getEndUnit());
        Unit ehnop;
        if(try2nop.containsKey(gb))
          ehnop = try2nop.get(gb);
        else
        {
          ehnop = new EHNopStmt();
          try2nop.put(gb, ehnop);
        }*/
       
       
        Unit ehnop;
        if(try2nop.containsKey(trap.getBeginUnit()))
          ehnop = try2nop.get(trap.getBeginUnit());
        else
        {
          ehnop = new EHNopStmt();
          try2nop.put(trap.getBeginUnit(), ehnop);
        }
               
    }
   
    //Only add a nop once
    Hashtable<Unit, Boolean> nop2added = new Hashtable<Unit, Boolean>();

    // Now actually add the edge
AddExceptionalEdge:
    for (Iterator<Trap> trapIt = body.getTraps().iterator(); trapIt.hasNext(); )
    {

        Trap trap = trapIt.next();
        Unit b = trap.getBeginUnit();
        Unit handler = trap.getHandlerUnit();
        handler = handler2header.get(handler);
       
       
        /**
         * Check if this trap is a finally trap that handles exceptions of an adjacent catch block;
         * what differentiates such trap is that it's guarded region has the same parent as the
         * handler of the trap itself, in the dom tree.
         *
         * The problem is that we don't have a complete DOM tree at this transient state.
         *
         * The work-around is to not process a trap that has already an edge pointing to it.
         *
         */
       
        if(this.unitToPreds.containsKey(handler))
        {
          List<Unit> handlerPreds = this.unitToPreds.get(handler);
          for(Iterator<Unit> preditr = handlerPreds.iterator(); preditr.hasNext(); )
            if(try2nop.containsValue(preditr.next()))
              continue AddExceptionalEdge;
             
        }
        else
          continue;

       
       
        //GuardedBlock gb = new GuardedBlock(b, e);
        Unit ehnop = try2nop.get(b);
               
        if(!nop2added.containsKey(ehnop))
        {
          List<Unit> predsOfB = getPredsOf(b);
          List<Unit> predsOfehnop = new ArrayList<Unit>(predsOfB);
         
          for(Iterator<Unit> itr = predsOfB.iterator(); itr.hasNext(); )
          {
            Unit a = itr.next();
            List<Unit> succsOfA = this.unitToSuccs.get(a);
            succsOfA.remove(b);
            succsOfA.add((Unit)ehnop);
          }
         
View Full Code Here

    {
      Region r = itr.next();
      List<Unit> units = r.getUnits();
      for (Iterator<Unit> itr1 = units.iterator(); itr1.hasNext();)
      {
        Unit u = itr1.next();
        unit2region.put(u, r);
       
      }
    }
   
View Full Code Here

        while (_notDone) {
            _notDone = false;

            for (Iterator units = body.getUnits().iterator(); units.hasNext();) {
                Unit unit = (Unit) units.next();

                if (unit instanceof DefinitionStmt) {
                    DefinitionStmt stmt = (DefinitionStmt) unit;
                    Value rightValue = stmt.getRightOp();
View Full Code Here

            }

            Iterator j = newBody.getUnits().iterator();

            while (j.hasNext()) {
                Unit unit = (Unit) j.next();

                // System.out.println("unit = " + unit);
                Iterator boxes = unit.getUseBoxes().iterator();

                while (boxes.hasNext()) {
                    ValueBox box = (ValueBox) boxes.next();
                    Value value = box.getValue();
View Full Code Here

            Body newBody = newMethod.retrieveActiveBody();
            Iterator j = newBody.getUnits().iterator();

            while (j.hasNext()) {
                Unit unit = (Unit) j.next();
                Iterator boxes = unit.getUseAndDefBoxes().iterator();

                while (boxes.hasNext()) {
                    ValueBox box = (ValueBox) boxes.next();
                    Value value = box.getValue();
View Full Code Here

                //Hierarchy hierarchy = Scene.v().getActiveHierarchy();

                // First inline all the methods that execute on Tokens.
                for (Iterator units = body.getUnits().snapshotIterator(); units
                        .hasNext();) {
                    Unit unit = (Unit) units.next();

                    if (debug) {
                        System.out.println("ttn1 unit = " + unit);
                    }

                    Iterator boxes = unit.getUseBoxes().iterator();

                    while (boxes.hasNext()) {
                        ValueBox box = (ValueBox) boxes.next();
                        /*Value value =*/box.getValue();

 
View Full Code Here

            // Go back again and replace references to fields
            // in the token with references to local
            // variables.
            for (Iterator units = body.getUnits().snapshotIterator(); units
                    .hasNext();) {
                Unit unit = (Unit) units.next();

                if (debug) {
                    System.out.println("ttn2 unit = " + unit);
                }

                if (unit instanceof InvokeStmt) {
                    // Handle java.lang.arraycopy
                    InvokeExpr r = (InvokeExpr) ((InvokeStmt) unit)
                            .getInvokeExpr();

                    if (r.getMethod().equals(PtolemyUtilities.arraycopyMethod)) {
                        if (debug) {
                            System.out.println("handling as array copy");
                        }

                        Local toLocal = (Local) r.getArg(0);
                        Local fromLocal = (Local) r.getArg(2);
                        Map toFieldToReplacementLocal = (Map) localToFieldToLocal
                                .get(toLocal);
                        Map fromFieldToReplacementLocal = (Map) localToFieldToLocal
                                .get(fromLocal);

                        if ((toFieldToReplacementLocal != null)
                                && (fromFieldToReplacementLocal != null)) {
                            if (debug) {
                                System.out
                                        .println("toFieldToReplacementLocal = "
                                                + toFieldToReplacementLocal);
                            }

                            if (debug) {
                                System.out
                                        .println("fromFieldToReplacementLocal = "
                                                + fromFieldToReplacementLocal);
                            }

                            {
                                List argumentList = new LinkedList();
                                argumentList.add((Local) localToIsNotNullLocal
                                        .get(toLocal));
                                argumentList.add(r.getArg(1));
                                argumentList.add((Local) localToIsNotNullLocal
                                        .get(fromLocal));
                                argumentList.add(r.getArg(3));
                                argumentList.add(r.getArg(4));

                                body
                                        .getUnits()
                                        .insertBefore(
                                                Jimple
                                                        .v()
                                                        .newInvokeStmt(
                                                                Jimple
                                                                        .v()
                                                                        .newStaticInvokeExpr(
                                                                                PtolemyUtilities.arraycopyMethod
                                                                                        .makeRef(),
                                                                                argumentList)),
                                                unit);
                            }

                            for (Iterator tokenFields = toFieldToReplacementLocal
                                    .keySet().iterator(); tokenFields.hasNext();) {
                                SootField tokenField = (SootField) tokenFields
                                        .next();

                                Local toReplacementLocal = (Local) toFieldToReplacementLocal
                                        .get(tokenField);
                                Local fromReplacementLocal = (Local) fromFieldToReplacementLocal
                                        .get(tokenField);
                                List argumentList = new LinkedList();
                                argumentList.add(toReplacementLocal);
                                argumentList.add(r.getArg(1));
                                argumentList.add(fromReplacementLocal);
                                argumentList.add(r.getArg(3));
                                argumentList.add(r.getArg(4));

                                body
                                        .getUnits()
                                        .insertBefore(
                                                Jimple
                                                        .v()
                                                        .newInvokeStmt(
                                                                Jimple
                                                                        .v()
                                                                        .newStaticInvokeExpr(
                                                                                PtolemyUtilities.arraycopyMethod
                                                                                        .makeRef(),
                                                                                argumentList)),
                                                unit);
                            }

                            body.getUnits().remove(unit);
                            doneSomething = true;
                        }
                    }
                } else if (unit instanceof AssignStmt) {
                    AssignStmt stmt = (AssignStmt) unit;
                    /*Type assignmentType =*/stmt.getLeftOp().getType();

                    if (stmt.getLeftOp() instanceof Local
                            && stmt.getRightOp() instanceof LengthExpr) {
                        if (debug) {
                            System.out.println("handling as length expr");
                        }

                        LengthExpr lengthExpr = (LengthExpr) stmt.getRightOp();
                        Local baseLocal = (Local) lengthExpr.getOp();

                        if (debug) {
                            System.out.println("operating on " + baseLocal);
                        }

                        Map fieldToReplacementArrayLocal = (Map) localToFieldToLocal
                                .get(baseLocal);

                        if (fieldToReplacementArrayLocal != null) {
                            doneSomething = true;

                            // Get the length of a random one of the replacement fields.
                            List replacementList = new ArrayList(
                                    fieldToReplacementArrayLocal.keySet());
                            Collections.sort(replacementList, new Comparator() {
                                public int compare(Object o1, Object o2) {
                                    SootField f1 = (SootField) o1;
                                    SootField f2 = (SootField) o2;
                                    return f1.getName().compareTo(f2.getName());
                                }
                            });

                            SootField field = (SootField) replacementList
                                    .get(replacementList.size() - 1);

                            if (debug) {
                                System.out.println("replace with  "
                                        + fieldToReplacementArrayLocal
                                                .get(field));
                            }

                            lengthExpr
                                    .setOp((Local) fieldToReplacementArrayLocal
                                            .get(field));

                            if (debug) {
                                System.out.println("unit now = " + unit);
                            }

                            //    body.getUnits().remove(unit);
                        }
                    } else if (stmt.getLeftOp() instanceof InstanceFieldRef) {
                        // Replace references to fields of tokens.
                        // FIXME: assign to all aliases as well.
                        if (debug) {
                            System.out
                                    .println("is assignment to Instance FieldRef");
                        }

                        InstanceFieldRef r = (InstanceFieldRef) stmt
                                .getLeftOp();
                        SootField field = r.getField();

                        if (r.getBase().getType() instanceof RefType) {
                            RefType type = (RefType) r.getBase().getType();

                            //System.out.println("BaseType = " + type);
                            if (SootUtilities.derivesFrom(type.getSootClass(),
                                    PtolemyUtilities.tokenClass)) {
                                if (debug) {
                                    System.out.println("handling " + unit
                                            + " token operation");
                                }

                                // We have a reference to a field of a token class.
                                Local baseLocal = (Local) r.getBase();
                                Local instanceLocal = _getInstanceLocal(body,
                                        baseLocal, field, localToFieldToLocal,
                                        debug);

                                if (debug) {
                                    System.out.println("instanceLocal = "
                                            + instanceLocal);
                                }

                                if (instanceLocal != null) {
                                    stmt.getLeftOpBox().setValue(instanceLocal);
                                    doneSomething = true;
                                }
                            }
                        }
                    } else if (stmt.getRightOp() instanceof InstanceFieldRef) {
                        // Replace references to fields of tokens.
                        if (debug) {
                            System.out
                                    .println("is assignment from Instance FieldRef");
                        }

                        InstanceFieldRef r = (InstanceFieldRef) stmt
                                .getRightOp();
                        SootField field = r.getField();

                        if (r.getBase().getType() instanceof RefType) {
                            RefType type = (RefType) r.getBase().getType();

                            //System.out.println("BaseType = " + type);
                            if (SootUtilities.derivesFrom(type.getSootClass(),
                                    PtolemyUtilities.tokenClass)) {
                                if (debug) {
                                    System.out.println("handling " + unit
                                            + " token operation");
                                }

                                // We have a reference to a field of a token class.
                                Local baseLocal = (Local) r.getBase();
                                Local instanceLocal = _getInstanceLocal(body,
                                        baseLocal, field, localToFieldToLocal,
                                        debug);

                                if (debug) {
                                    System.out.println("instanceLocal = "
                                            + instanceLocal);
                                }

                                if (instanceLocal != null) {
                                    stmt.getRightOpBox()
                                            .setValue(instanceLocal);
                                    doneSomething = true;
                                }
                            }
                        }
                    }
                }
            }
        }

        if (debug) {
            System.out.println("Specializing types for " + entityClass);
        }

        // Specialize the token types.  Any field we created above
        // should now have its correct concrete type.
        // TypeSpecializer.specializeTypes(debug, entityClass, unsafeLocalSet);
        // Now go through the methods again and handle all token assignments,
        // replacing them with assignments on the native replacements.
        for (Iterator methods = entityClass.getMethods().iterator(); methods
                .hasNext();) {
            SootMethod method = (SootMethod) methods.next();

            if (debug) {
                System.out.println("Replacing token assignments in method "
                        + method);
            }

            JimpleBody body = (JimpleBody) method.retrieveActiveBody();

            //   InstanceEqualityEliminator.removeInstanceEqualities(body, null, true);
            for (Iterator units = body.getUnits().snapshotIterator(); units
                    .hasNext();) {
                Unit unit = (Unit) units.next();

                if (debug) {
                    System.out.println("ttn3 unit = " + unit);
                }

                // Hack to work around the presence of NIL fields:
                // Assume they are null references.
                //      for (Iterator boxes = unit.getUseAndDefBoxes().iterator(); boxes
                //                         .hasNext();) {
                //                     ValueBox box = (ValueBox) boxes.next();
                //                     Value value = box.getValue();
                //                     if (value instanceof FieldRef) {
                //                         FieldRef ref = (FieldRef)value;
                //                         SootField field = ref.getField();
                //                          if (field.getName().equals("NIL")) {
                //                              doneSomething = true;

                //                              box.setValue(NullConstant.v());
                //                              System.out.println("replacing as Null");
                //                          }
                //                     }
                //                 }

                if (unit instanceof AssignStmt) {
                    AssignStmt stmt = (AssignStmt) unit;
                    Type assignmentType = stmt.getLeftOp().getType();

                    if (PtolemyUtilities.isTokenType(assignmentType)) {
                        if (stmt.getLeftOp() instanceof Local
                                && (stmt.getRightOp() instanceof Local || stmt
                                        .getRightOp() instanceof Constant)) {
                            if (debug) {
                                System.out
                                        .println("handling as local-immediate assign");
                            }

                            doneSomething |= _handleImmediateAssignment(body,
                                    stmt, localToFieldToLocal,
                                    localToIsNotNullLocal, stmt.getLeftOp(),
                                    stmt.getRightOp(), debug);
                        } else if (stmt.getLeftOp() instanceof Local
                                && stmt.getRightOp() instanceof CastExpr) {
                            if (debug) {
                                System.out.println("handling as local cast");
                            }

                            Value rightLocal = ((CastExpr) stmt.getRightOp())
                                    .getOp();

                            doneSomething |= _handleImmediateAssignment(body,
                                    stmt, localToFieldToLocal,
                                    localToIsNotNullLocal, stmt.getLeftOp(),
                                    rightLocal, debug);
                        } else if (stmt.getLeftOp() instanceof FieldRef
                                && stmt.getRightOp() instanceof Local) {
                            if (debug) {
                                System.out
                                        .println("handling as assignment to Field");
                            }

                            FieldRef oldFieldRef = (FieldRef) stmt.getLeftOp();
                            SootField field = oldFieldRef.getField();
                            Map fieldToReplacementField = (Map) entityFieldToTokenFieldToReplacementField
                                    .get(field);
                            Map fieldToReplacementLocal = (Map) localToFieldToLocal
                                    .get(stmt.getRightOp());

                            //        System.out.println("fieldToReplacementField = " + fieldToReplacementField);
                            //                             System.out.println("fieldToReplacementLocal = " + fieldToReplacementLocal);
                            if ((fieldToReplacementLocal != null)
                                    && (fieldToReplacementField != null)) {
                                doneSomething = true;
                                // Replace references to fields with token types.
                                {
                                    SootField replacementField = (SootField) entityFieldToIsNotNullField
                                            .get(field);

                                    //System.out.println("replacementField = " + replacementField);
                                    FieldRef isNotNullFieldRef;

                                    if (oldFieldRef instanceof InstanceFieldRef) {
                                        isNotNullFieldRef = Jimple
                                                .v()
                                                .newInstanceFieldRef(
                                                        ((InstanceFieldRef) oldFieldRef)
                                                                .getBase(),
                                                        replacementField
                                                                .makeRef());
                                    } else {
                                        isNotNullFieldRef = Jimple.v()
                                                .newStaticFieldRef(
                                                        replacementField
                                                                .makeRef());
                                    }

                                    body
                                            .getUnits()
                                            .insertBefore(
                                                    Jimple
                                                            .v()
                                                            .newAssignStmt(
                                                                    isNotNullFieldRef,
                                                                    (Local) localToIsNotNullLocal
                                                                            .get(stmt
                                                                                    .getRightOp())),
                                                    unit);
                                }

                                if (debug) {
                                    System.out.println("local = "
                                            + stmt.getLeftOp());
                                }

                                for (Iterator tokenFields = fieldToReplacementField
                                        .keySet().iterator(); tokenFields
                                        .hasNext();) {
                                    SootField tokenField = (SootField) tokenFields
                                            .next();

                                    if (debug) {
                                        System.out.println("tokenField = "
                                                + tokenField);
                                    }

                                    SootField replacementField = (SootField) fieldToReplacementField
                                            .get(tokenField);
                                    Local replacementLocal = (Local) fieldToReplacementLocal
                                            .get(tokenField);

                                    if (debug) {
                                        System.out
                                                .println("replacementLocal = "
                                                        + replacementLocal);
                                    }

                                    FieldRef fieldRef;

                                    if (stmt.getLeftOp() instanceof InstanceFieldRef) {
                                        Local base = (Local) ((InstanceFieldRef) stmt
                                                .getLeftOp()).getBase();
                                        fieldRef = Jimple.v()
                                                .newInstanceFieldRef(
                                                        base,
                                                        replacementField
                                                                .makeRef());
                                    } else {
                                        fieldRef = Jimple.v()
                                                .newStaticFieldRef(
                                                        replacementField
                                                                .makeRef());
                                    }

                                    body.getUnits().insertBefore(
                                            Jimple.v().newAssignStmt(fieldRef,
                                                    replacementLocal), unit);
                                }

                                stmt.getRightOpBox().setValue(NullConstant.v());

                                // body.getUnits().remove(unit);
                            }
                        } else if (stmt.getLeftOp() instanceof Local
                                && stmt.getRightOp() instanceof FieldRef) {
                            if (debug) {
                                System.out
                                        .println("handling as assignment from Field");
                            }

                            FieldRef oldFieldRef = (FieldRef) stmt.getRightOp();
                            Map fieldToReplacementLocal = (Map) localToFieldToLocal
                                    .get(stmt.getLeftOp());
                            SootField field = oldFieldRef.getField();
                            Map fieldToReplacementField = (Map) entityFieldToTokenFieldToReplacementField
                                    .get(field);

                            // There are some fields that represent
                            // singleton tokens.  Deal with them
                            // specially.

                            // This is awkward, but it is simpler than
                            // doing a dataflow analysis of the static
                            // initializer of the token class to
                            // figure out what the value is.
                            boolean isSingleton = false;
                            if (field.getName().equals("TRUE")
                                    || field.getName().equals("FALSE")
                                    || field.getName().equals("ZERO")
                                    || field.getName().equals("ONE")) {
                                isSingleton = true;
                            }

                            if ((isSingleton)
                                    && (fieldToReplacementLocal != null)) {
                                doneSomething = true;

                                // Replace references to fields with
                                // token types.  The special fields
                                // should never be null
                                body.getUnits().insertBefore(
                                        Jimple.v().newAssignStmt(
                                                (Local) localToIsNotNullLocal
                                                        .get(stmt.getLeftOp()),
                                                IntConstant.v(1)), unit);

                                if (debug) {
                                    System.out.println("local = "
                                            + stmt.getLeftOp());
                                }

                                for (Iterator localFields = fieldToReplacementLocal
                                        .keySet().iterator(); localFields
                                        .hasNext();) {
                                    SootField localField = (SootField) localFields
                                            .next();

                                    if (debug) {
                                        System.out.println("localField = "
                                                + localField);
                                    }

                                    //     if (localField.getName().equals("_isNil")) {
                                    //                                         Local replacementLocal = (Local) fieldToReplacementLocal
                                    //                                                 .get(localField);

                                    //                                         body
                                    //                                                 .getUnits()
                                    //                                                 .insertBefore(
                                    //                                                         Jimple
                                    //                                                                 .v()
                                    //                                                                 .newAssignStmt(
                                    //                                                                         replacementLocal,
                                    //                                                                         IntConstant
                                    //                                                                                 .v(1)),
                                    //                                                         unit);
                                    //                                     } else
                                    if (localField.getName().equals(
                                            "_unitCategoryExponents")) {

                                        Local replacementLocal = (Local) fieldToReplacementLocal
                                                .get(localField);
                                        body
                                                .getUnits()
                                                .insertBefore(
                                                        Jimple
                                                                .v()
                                                                .newAssignStmt(
                                                                        replacementLocal,
                                                                        NullConstant
                                                                                .v()),
                                                        unit);
                                    } else if (localField.getName().equals(
                                            "_value")) {

                                        Local replacementLocal = (Local) fieldToReplacementLocal
                                                .get(localField);

                                        if (field
                                                .getSignature()
                                                .equals(
                                                        "<ptolemy.data.BooleanToken: ptolemy.data.BooleanToken TRUE>")) {
                                            body.getUnits().insertBefore(
                                                    Jimple.v().newAssignStmt(
                                                            replacementLocal,
                                                            IntConstant.v(1)),
                                                    unit);
                                        } else if (field
                                                .getSignature()
                                                .equals(
                                                        "<ptolemy.data.BooleanToken: ptolemy.data.BooleanToken FALSE>")) {

                                            body.getUnits().insertBefore(
                                                    Jimple.v().newAssignStmt(
                                                            replacementLocal,
                                                            IntConstant.v(0)),
                                                    unit);
                                        } else if (field
                                                .getSignature()
                                                .equals(
                                                        "<ptolemy.data.UnsignedByteToken: ptolemy.data.UnsignedByteToken ONE>")) {
                                            body.getUnits().insertBefore(
                                                    Jimple.v().newAssignStmt(
                                                            replacementLocal,
                                                            IntConstant.v(1)),
                                                    unit);
                                        } else if (field
                                                .getSignature()
                                                .equals(
                                                        "<ptolemy.data.UnsignedByteToken: ptolemy.data.UnsignedByteToken ZERO>")) {

                                            body.getUnits().insertBefore(
                                                    Jimple.v().newAssignStmt(
                                                            replacementLocal,
                                                            IntConstant.v(0)),
                                                    unit);
                                        } else if (field
                                                .getSignature()
                                                .equals(
                                                        "<ptolemy.data.IntToken: ptolemy.data.IntToken ONE>")) {
                                            body.getUnits().insertBefore(
                                                    Jimple.v().newAssignStmt(
                                                            replacementLocal,
                                                            IntConstant.v(1)),
                                                    unit);
                                        } else if (field
                                                .getSignature()
                                                .equals(
                                                        "<ptolemy.data.IntToken: ptolemy.data.IntToken ZERO>")) {

                                            body.getUnits().insertBefore(
                                                    Jimple.v().newAssignStmt(
                                                            replacementLocal,
                                                            IntConstant.v(0)),
                                                    unit);
                                        } else if (field
                                                .getSignature()
                                                .equals(
                                                        "<ptolemy.data.LongToken: ptolemy.data.LongToken ONE>")) {
                                            body.getUnits().insertBefore(
                                                    Jimple.v().newAssignStmt(
                                                            replacementLocal,
                                                            LongConstant.v(1)),
                                                    unit);
                                        } else if (field
                                                .getSignature()
                                                .equals(
                                                        "<ptolemy.data.LongToken: ptolemy.data.LongToken ZERO>")) {

                                            body.getUnits().insertBefore(
                                                    Jimple.v().newAssignStmt(
                                                            replacementLocal,
                                                            LongConstant.v(0)),
                                                    unit);
                                        } else if (field
                                                .getSignature()
                                                .equals(
                                                        "<ptolemy.data.DoubleToken: ptolemy.data.DoubleToken ONE>")) {

                                            body.getUnits().insertBefore(
                                                    Jimple.v().newAssignStmt(
                                                            replacementLocal,
                                                            DoubleConstant
                                                                    .v(1.0)),
                                                    unit);
                                        } else if (field
                                                .getSignature()
                                                .equals(
                                                        "<ptolemy.data.DoubleToken: ptolemy.data.DoubleToken ZERO>")) {

                                            body.getUnits().insertBefore(
                                                    Jimple.v().newAssignStmt(
                                                            replacementLocal,
                                                            DoubleConstant
                                                                    .v(0.0)),
                                                    unit);
                                        }
                                    } else {
                                        throw new RuntimeException(
                                                "Unknown Field in Token: "
                                                        + localField
                                                                .getSignature());
                                    }
                                }

                                stmt.getRightOpBox().setValue(NullConstant.v());

                                //body.getUnits().remove(unit);
                            } else if ((fieldToReplacementLocal != null)
                                    && (fieldToReplacementField != null)) {
                                doneSomething = true;
                                // Replace references to fields with token types.
                                // FIXME properly handle isNotNull field?
                                {
                                    SootField replacementField = (SootField) entityFieldToIsNotNullField
                                            .get(field);

                                    //   System.out.println("replacementField = " + replacementField);
                                    FieldRef isNotNullFieldRef;

                                    if (oldFieldRef instanceof InstanceFieldRef) {
                                        isNotNullFieldRef = Jimple
                                                .v()
                                                .newInstanceFieldRef(
                                                        ((InstanceFieldRef) oldFieldRef)
                                                                .getBase(),
                                                        replacementField
                                                                .makeRef());
                                    } else {
                                        isNotNullFieldRef = Jimple.v()
                                                .newStaticFieldRef(
                                                        replacementField
                                                                .makeRef());
                                    }

                                    body
                                            .getUnits()
                                            .insertBefore(
                                                    Jimple
                                                            .v()
                                                            .newAssignStmt(
                                                                    (Local) localToIsNotNullLocal
                                                                            .get(stmt
                                                                                    .getLeftOp()),
                                                                    isNotNullFieldRef),
                                                    unit);
                                }

                                if (debug) {
                                    System.out.println("local = "
                                            + stmt.getLeftOp());
                                }

                                for (Iterator tokenFields = fieldToReplacementField
                                        .keySet().iterator(); tokenFields
                                        .hasNext();) {
                                    SootField tokenField = (SootField) tokenFields
                                            .next();

                                    if (debug) {
                                        System.out.println("tokenField = "
                                                + tokenField);
                                    }

                                    SootField replacementField = (SootField) fieldToReplacementField
                                            .get(tokenField);
                                    Local replacementLocal = (Local) fieldToReplacementLocal
                                            .get(tokenField);
                                    FieldRef fieldRef;

                                    if (stmt.getRightOp() instanceof InstanceFieldRef) {
                                        Local base = (Local) ((InstanceFieldRef) stmt
                                                .getRightOp()).getBase();
                                        fieldRef = Jimple.v()
                                                .newInstanceFieldRef(
                                                        base,
                                                        replacementField
                                                                .makeRef());
                                    } else {
                                        fieldRef = Jimple.v()
                                                .newStaticFieldRef(
                                                        replacementField
                                                                .makeRef());
                                    }

                                    if (debug) {
                                        System.out
                                                .println("replacementLocal = "
                                                        + replacementLocal);
                                    }

                                    body.getUnits()
                                            .insertBefore(
                                                    Jimple.v().newAssignStmt(
                                                            replacementLocal,
                                                            fieldRef), unit);
                                }

                                stmt.getRightOpBox().setValue(NullConstant.v());

                                //      body.getUnits().remove(unit);
                            }
                        } else if (stmt.getLeftOp() instanceof ArrayRef
                                && stmt.getRightOp() instanceof Local) {
                            if (debug) {
                                System.out
                                        .println("handling as assignment to Array");
                            }

                            ArrayRef arrayRef = (ArrayRef) stmt.getLeftOp();
                            Local baseLocal = (Local) arrayRef.getBase();
                            Map fieldToReplacementArrayLocal = (Map) localToFieldToLocal
                                    .get(baseLocal);
                            Map fieldToReplacementLocal = (Map) localToFieldToLocal
                                    .get(stmt.getRightOp());

                            if (debug) {
                                System.out
                                        .println("fieldToReplacementArrayLocal = "
                                                + fieldToReplacementArrayLocal);
                            }

                            if (debug) {
                                System.out.println("fieldToReplacementLocal = "
                                        + fieldToReplacementLocal);
                            }

                            if ((fieldToReplacementLocal != null)
                                    && (fieldToReplacementArrayLocal != null)) {
                                doneSomething = true;
                                body
                                        .getUnits()
                                        .insertBefore(
                                                Jimple
                                                        .v()
                                                        .newAssignStmt(
                                                                Jimple
                                                                        .v()
                                                                        .newArrayRef(
                                                                                (Local) localToIsNotNullLocal
                                                                                        .get(baseLocal),
                                                                                arrayRef
                                                                                        .getIndex()),
                                                                (Local) localToIsNotNullLocal
                                                                        .get(stmt
                                                                                .getRightOp())),
                                                unit);

                                if (debug) {
                                    System.out.println("local = "
                                            + stmt.getLeftOp());
                                }

                                for (Iterator tokenFields = fieldToReplacementLocal
                                        .keySet().iterator(); tokenFields
                                        .hasNext();) {
                                    SootField tokenField = (SootField) tokenFields
                                            .next();

                                    if (debug) {
                                        System.out.println("tokenField = "
                                                + tokenField);
                                    }

                                    Local replacementArrayLocal = (Local) fieldToReplacementArrayLocal
                                            .get(tokenField);
                                    Local replacementLocal = (Local) fieldToReplacementLocal
                                            .get(tokenField);

                                    body
                                            .getUnits()
                                            .insertBefore(
                                                    Jimple
                                                            .v()
                                                            .newAssignStmt(
                                                                    Jimple
                                                                            .v()
                                                                            .newArrayRef(
                                                                                    replacementArrayLocal,
                                                                                    arrayRef
                                                                                            .getIndex()),
                                                                    replacementLocal),
                                                    unit);
                                }

                                // Have to remove here, because otherwise we'll try to
                                // index into a null array.
                                //stmt.getRightOpBox().setValue(NullConstant.v());
                                body.getUnits().remove(unit);
                            }
                        } else if (stmt.getLeftOp() instanceof Local
                                && stmt.getRightOp() instanceof ArrayRef) {
                            if (debug) {
                                System.out
                                        .println("handling as assignment from Array");
                            }

                            ArrayRef arrayRef = (ArrayRef) stmt.getRightOp();
                            Map fieldToReplacementLocal = (Map) localToFieldToLocal
                                    .get(stmt.getLeftOp());
                            Local baseLocal = (Local) arrayRef.getBase();
                            Map fieldToReplacementArrayLocal = (Map) localToFieldToLocal
                                    .get(baseLocal);

                            if ((fieldToReplacementLocal != null)
                                    && (fieldToReplacementArrayLocal != null)) {
                                doneSomething = true;

                                body
                                        .getUnits()
                                        .insertBefore(
                                                Jimple
                                                        .v()
                                                        .newAssignStmt(
                                                                (Local) localToIsNotNullLocal
                                                                        .get(stmt
                                                                                .getLeftOp()),
                                                                Jimple
                                                                        .v()
                                                                        .newArrayRef(
                                                                                (Local) localToIsNotNullLocal
                                                                                        .get(baseLocal),
                                                                                arrayRef
                                                                                        .getIndex())),
                                                unit);

                                if (debug) {
                                    System.out.println("local = "
                                            + stmt.getLeftOp());
                                }

                                for (Iterator tokenFields = fieldToReplacementLocal
                                        .keySet().iterator(); tokenFields
                                        .hasNext();) {
                                    SootField tokenField = (SootField) tokenFields
                                            .next();

                                    if (debug) {
                                        System.out.println("tokenField = "
                                                + tokenField);
                                    }

                                    Local replacementArrayLocal = (Local) fieldToReplacementArrayLocal
                                            .get(tokenField);
                                    Local replacementLocal = (Local) fieldToReplacementLocal
                                            .get(tokenField);

                                    if (debug) {
                                        System.out
                                                .println("replacementLocal = "
                                                        + replacementLocal);
                                    }

                                    if (debug) {
                                        System.out
                                                .println("replacementArrayLocal = "
                                                        + replacementArrayLocal);
                                    }

                                    body
                                            .getUnits()
                                            .insertBefore(
                                                    Jimple
                                                            .v()
                                                            .newAssignStmt(
                                                                    replacementLocal,
                                                                    Jimple
                                                                            .v()
                                                                            .newArrayRef(
                                                                                    replacementArrayLocal,
                                                                                    arrayRef
                                                                                            .getIndex())),
                                                    unit);
                                }

                                stmt.getRightOpBox().setValue(NullConstant.v());

                                //body.getUnits().remove(unit);
                            }
                        } else if (stmt.getLeftOp() instanceof Local
                                && stmt.getRightOp() instanceof NewArrayExpr) {
                            if (debug) {
                                System.out
                                        .println("handling as new array object");
                            }

                            NewArrayExpr newExpr = (NewArrayExpr) stmt
                                    .getRightOp();

                            // We have an assignment to a local from a new array.
                            Map map = (Map) localToFieldToLocal.get(stmt
                                    .getLeftOp());

                            if (map != null) {
                                doneSomething = true;

                                Type isNotNullType = SootUtilities
                                        .createIsomorphicType(newExpr
                                                .getBaseType(), BooleanType.v());
                                Local isNotNullLocal = (Local) localToIsNotNullLocal
                                        .get(stmt.getLeftOp());
                                body.getUnits().insertBefore(
                                        Jimple.v().newAssignStmt(
                                                isNotNullLocal,
                                                Jimple.v().newNewArrayExpr(
                                                        isNotNullType,
                                                        newExpr.getSize())),
                                        unit);

                                for (Iterator tokenFields = map.keySet()
                                        .iterator(); tokenFields.hasNext();) {
                                    SootField tokenField = (SootField) tokenFields
                                            .next();

                                    if (debug) {
                                        System.out.println("tokenField = "
                                                + tokenField);
                                    }

                                    Local replacementLocal = (Local) map
                                            .get(tokenField);

                                    Type replacementType = SootUtilities
                                            .createIsomorphicType(newExpr
                                                    .getBaseType(), tokenField
                                                    .getType());

                                    // Initialize fields?
                                    body
                                            .getUnits()
                                            .insertBefore(
                                                    Jimple
                                                            .v()
                                                            .newAssignStmt(
                                                                    replacementLocal,
                                                                    Jimple
                                                                            .v()
                                                                            .newNewArrayExpr(
                                                                                    replacementType,
                                                                                    newExpr
                                                                                            .getSize())),
                                                    unit);
                                }

                                stmt.getRightOpBox().setValue(NullConstant.v());

                                //body.getUnits().remove(unit);
                            }
                        } else if (stmt.getLeftOp() instanceof Local
                                && stmt.getRightOp() instanceof NewExpr) {
                            if (debug) {
                                System.out.println("handling as new object");
                            }

                            /* NewExpr newExpr = (NewExpr)*/stmt.getRightOp();

                            // We have an assignment from one local token to another.
                            Map map = (Map) localToFieldToLocal.get(stmt
                                    .getLeftOp());

                            if (map != null) {
                                doneSomething = true;

                                Local isNotNullLocal = (Local) localToIsNotNullLocal
                                        .get(stmt.getLeftOp());

                                if (debug) {
                                    System.out.println("Stmt = " + stmt);
                                }

                                body.getUnits().insertBefore(
                                        Jimple.v().newAssignStmt(
                                                isNotNullLocal,
                                                IntConstant.v(1)), unit);

                                for (Iterator tokenFields = map.keySet()
                                        .iterator(); tokenFields.hasNext();) {
                                    SootField tokenField = (SootField) tokenFields
                                            .next();
                                    Local replacementLocal = (Local) map
                                            .get(tokenField);

                                    // Initialize fields?
                                    if (debug) {
                                        System.out.println("tokenField = "
                                                + tokenField);
                                    }

                                    // FIXME: ??
                                    Value replacementValue = _getNullValueForType(replacementLocal
                                            .getType());
                                    body.getUnits().insertBefore(
                                            Jimple.v().newAssignStmt(
                                                    replacementLocal,
                                                    replacementValue), unit);
                                }

                                stmt.getRightOpBox().setValue(NullConstant.v());

                                // body.getUnits().remove(unit);
                            }
                        }

                        //  else if (stmt.getLeftOp() instanceof Local &&
                        //                                        stmt.getRightOp() instanceof InvokeExpr) {
                        //                                  System.out.println("handling as method call.");
                        //                                  // We have an assignment from one local token to another.
                        //                                  Map map = (Map)localToFieldToLocal.get(stmt.getLeftOp());
                        //                                  if (map != null) {
                        //                                      Local isNotNullLocal = (Local)
                        //                                          localToIsNotNullLocal.get(stmt.getLeftOp());
                        //                                      body.getUnits().insertAfter(
                        //                                              Jimple.v().newAssignStmt(
                        //                                                      isNotNullLocal,
                        //                                                      IntConstant.v(0)),
                        //                                              unit);
                        //                                      for (Iterator tokenFields = map.keySet().iterator();
                        //                                          tokenFields.hasNext();) {
                        //                                          SootField tokenField = (SootField)tokenFields.next();
                        //                                          Local replacementLocal = (Local)
                        //                                              map.get(tokenField);
                        //                                          // Initialize fields?
                        //                                          body.getUnits().insertAfter(
                        //                                                  Jimple.v().newAssignStmt(
                        //                                                          replacementLocal,
                        //                                                          ),
                        //                                                  unit);
                        //                                      }
                        //                                  }
                        //                                  }
                    }
                }

                // FIXME!  This does not handle null values in fields well...
                for (Iterator boxes = unit.getUseAndDefBoxes().iterator(); boxes
                        .hasNext();) {
                    ValueBox box = (ValueBox) boxes.next();
                    Value value = box.getValue();

                    if (value instanceof BinopExpr) {
View Full Code Here

TOP

Related Classes of soot.Unit

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.