Examples of JimpleBody


Examples of soot.jimple.JimpleBody

        //when allowing phantom refs we also allow for references to non-existing methods;
        //we simply create the methods on the fly; the method body will throw an appropriate
        //error just in case the code *is* actually reached at runtime
        if(Options.v().allow_phantom_refs()) {
          SootMethod m = new SootMethod(name, parameterTypes, returnType);
          JimpleBody body = Jimple.v().newBody(m);
      m.setActiveBody(body);
     
      //exc = new Error
      RefType runtimeExceptionType = RefType.v("java.lang.Error");
      NewExpr newExpr = Jimple.v().newNewExpr(runtimeExceptionType);
      LocalGenerator lg = new LocalGenerator(body);
      Local exceptionLocal = lg.generateLocal(runtimeExceptionType);
      AssignStmt assignStmt = Jimple.v().newAssignStmt(exceptionLocal, newExpr);
      body.getUnits().add(assignStmt);
     
      //exc.<init>(message)
      SootMethodRef cref = runtimeExceptionType.getSootClass().getMethod("<init>", Collections.singletonList(RefType.v("java.lang.String"))).makeRef();
      SpecialInvokeExpr constructorInvokeExpr = Jimple.v().newSpecialInvokeExpr(exceptionLocal, cref, StringConstant.v("Unresolved compilation error: Method "+getSignature()+" does not exist!"));
      InvokeStmt initStmt = Jimple.v().newInvokeStmt(constructorInvokeExpr);
      body.getUnits().insertAfter(initStmt, assignStmt);
     
      //throw exc
      body.getUnits().insertAfter(Jimple.v().newThrowStmt(exceptionLocal), initStmt);

      declaringClass.addMethod(m);
      return m;
        } else if( trace == null ) {
          throw new ClassResolutionFailedException();
View Full Code Here

Examples of soot.jimple.JimpleBody

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

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

                    LocalSplitter.v().transform(body, _phaseName + ".ls");

                    // We may have locals with the same name.  Rename them.
                    LocalNameStandardizer.v().transform(body,
View Full Code Here

Examples of soot.jimple.JimpleBody

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

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

                // First split local variables that are used in
                // multiple places.
                LocalSplitter.v().transform(body, _phaseName + ".ls");
View Full Code Here

Examples of soot.jimple.JimpleBody

            // This will allow us to get a better type inference below.
            for (Iterator methods = entityClass.getMethods().iterator(); methods
                    .hasNext();) {
                SootMethod method = (SootMethod) methods.next();

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

                // First split local variables that are used in
                // multiple places.
                LocalSplitter.v().transform(body, _phaseName + ".ls");

                // We may have locals with the same name.  Rename them.
                LocalNameStandardizer.v().transform(body, _phaseName + ".lns");

                // Assign types to local variables... This types
                // everything that isn't a token type.
                TypeAssigner.v().transform(body, _phaseName + ".ta");

                // Run some cleanup...  this will speedup the rest
                // of the analysis.  And prevent typing errors.
                //     TokenInstanceofEliminator.eliminateCastsAndInstanceOf(
                //                         body, _phaseName + ".tie", unsafeLocalSet,
                //                         true);
                UnreachableCodeEliminator.v().transform(body,
                        _phaseName + ".uce");
                CopyPropagator.v().transform(body, _phaseName + ".cp");
                ConstantPropagatorAndFolder.v().transform(body,
                        _phaseName + ".cpf");
                ConditionalBranchFolder.v()
                        .transform(body, _phaseName + ".cbf");
                DeadAssignmentEliminator.v().transform(body,
                        _phaseName + ".dae");
                UnreachableCodeEliminator.v().transform(body,
                        _phaseName + ".uce");
                UnusedLocalEliminator.v().transform(body, _phaseName + ".ule");

                // Run some cleanup...  this will speedup the rest
                // of the analysis.  And prevent typing errors.
                if (debug) {
                    System.out.println("eliminating instanceof in " + method);
                }

                TokenInstanceofEliminator.eliminateCastsAndInstanceOf(body,
                        _phaseName + ".tie", unsafeLocalSet, debug);

                UnreachableCodeEliminator.v().transform(body,
                        _phaseName + ".uce");
                CopyPropagator.v().transform(body, _phaseName + ".cp");
                ConstantPropagatorAndFolder.v().transform(body,
                        _phaseName + ".cpf");
                ConditionalBranchFolder.v()
                        .transform(body, _phaseName + ".cbf");
                DeadAssignmentEliminator.v().transform(body,
                        _phaseName + ".dae");
                UnusedLocalEliminator.v().transform(body, _phaseName + ".ule");
                UnreachableCodeEliminator.v().transform(body,
                        _phaseName + ".uce");
                CopyPropagator.v().transform(body, _phaseName + ".cp");
                ConstantPropagatorAndFolder.v().transform(body,
                        _phaseName + ".cpf");
                ConditionalBranchFolder.v()
                        .transform(body, _phaseName + ".cbf");
                DeadAssignmentEliminator.v().transform(body,
                        _phaseName + ".dae");
                UnusedLocalEliminator.v().transform(body, _phaseName + ".ule");
                LocalSplitter.v().transform(body, _phaseName + ".ls");
                TypeAssigner.v().transform(body, _phaseName + ".ta");
                TokenInstanceofEliminator.eliminateCastsAndInstanceOf(body,
                        _phaseName + ".cie", Collections.EMPTY_SET, debug);
            }

            // Now run the type specialization algorithm...  This
            // allows us to resolve the methods that we are inlining
            // with better precision.
            // Map objectToTokenType = TypeSpecializer.specializeTypes(
            //   debug, entityClass, unsafeLocalSet);
            TypeSpecializerAnalysis typeAnalysis = new TypeSpecializerAnalysis(
                    entityClass, unsafeLocalSet);

            // Specialize the code, according to the analyzed types.
            //TypeSpecializer.specializeTypes(true, entityClass, unsafeLocalSet, typeAnalysis);
            for (Iterator methods = entityClass.getMethods().iterator(); methods
                    .hasNext();) {
                SootMethod method = (SootMethod) methods.next();

                // Check to see if the method is safe to modify with:
                // It has no arguments or return values which are
                // tokens.
                if (_methodWillBeInlined(method)) {
                    continue;
                }

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

                CompleteUnitGraph unitGraph = new CompleteUnitGraph(body);
                SimpleLocalDefs localDefs = new SimpleLocalDefs(unitGraph);
                SimpleLocalUses localUses = new SimpleLocalUses(unitGraph,
                        localDefs);

                //  TokenTypeAnalysis typeAnalysis =
                //    new TokenTypeAnalysis(method, unitGraph);
                if (debug) {
                    System.out.println("method = " + method);
                }

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

                    if (debug) {
                        System.out.println("ttn0 unit = " + stmt);
View Full Code Here

Examples of soot.jimple.JimpleBody

            for (Iterator methods = entityClass.getMethods().iterator(); methods
                    .hasNext();) {
                SootMethod method = (SootMethod) methods.next();

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

                CompleteUnitGraph unitGraph = new CompleteUnitGraph(body);
                SimpleLocalDefs localDefs = new SimpleLocalDefs(unitGraph);
                SimpleLocalUses localUses = new SimpleLocalUses(unitGraph,
                        localDefs);

                //  TokenTypeAnalysis typeAnalysis =
                //    new TokenTypeAnalysis(method, unitGraph);
                if (debug) {
                    System.out.println("method = " + method);
                }

                //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);
View Full Code Here

Examples of soot.jimple.JimpleBody

            // tokens.
            if (_methodWillBeInlined(method)) {
                continue;
            }

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

            //  UnitGraph graph = new CompleteUnitGraph(body);
            //  MustAliasAnalysis aliasAnalysis = new MustAliasAnalysis(graph);
            if (debug) {
                System.out.println("creating replacement locals in method = "
                        + method);
            }

            // A map from a local variable that references a
            // token and a field of that token class to the
            // local variable that will replace a
            // fieldReference to that field based on the
            // local.
            for (Iterator locals = body.getLocals().snapshotIterator(); locals
                    .hasNext();) {
                Local local = (Local) locals.next();
                Type localType = typeAnalysis.getSpecializedSootType(local);

                if (debug) {
                    System.out
                            .println("Attempting to create replacement fields for local = "
                                    + local);
                }

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

                // If the type is not a token, then skip it.
                if (!PtolemyUtilities.isConcreteTokenType(localType)
                        || unsafeLocalSet.contains(local)) {
                    if (debug) {
                        System.out
                                .println("skipping: unsafe or not concrete token");
                    }

                    continue;
                }

                ptolemy.data.type.Type localTokenType = typeAnalysis
                        .getSpecializedType(local);

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

                // Ignore fields that aren't of the right depth.
                if (PtolemyUtilities.getTypeDepth(localTokenType) != depth) {
                    if (debug) {
                        System.out
                                .println("skipping: depth is only "
                                        + PtolemyUtilities
                                                .getTypeDepth(localTokenType));
                    }

                    continue;
                }

                // If the type is not instantiable, then skip it.
                //    if (!localTokenType.isInstantiable()) {
                //    continue;
                // }
                // If we've already created subfields for this
                // field, then don't do it again.
                if (localToFieldToLocal.get(local) != null) {
                    continue;
                }

                RefType type = PtolemyUtilities.getBaseTokenType(localType);
                SootClass localClass = type.getSootClass();

                if (!SootUtilities.derivesFrom(localClass,
                        PtolemyUtilities.tokenClass)) {
                    if (debug) {
                        System.out.println("skipping: not a token.");
                    }

                    continue;
                }

                if (debug) {
                    System.out
                            .println("Creating replacement fields for local = "
                                    + local);
                }

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

                // We are going to make a modification
                doneSomething = true;

                Type isNotNullType = SootUtilities.createIsomorphicType(
                        localType, BooleanType.v());

                // Create a boolean value that tells us whether or
                // not the token is null.  Initialize it to true.
                Local isNotNullLocal = Jimple.v().newLocal(
                        local.getName() + "_isNotNull", isNotNullType);
                body.getLocals().add(isNotNullLocal);
                localToIsNotNullLocal.put(local, isNotNullLocal);

                // Note: default initialization is to false..
                //                 body.getUnits().insertBefore(
                //                         Jimple.v().newAssignStmt(
                //                                 isNotNullLocal,
                //                                 IntConstant.v(1)),
                //                         body.getFirstNonIdentityStmt());
                Map tokenFieldToReplacementLocal = new HashMap();
                localToFieldToLocal.put(local, tokenFieldToReplacementLocal);

                for (Iterator tokenFields = _getTokenClassFields(localClass)
                        .iterator(); tokenFields.hasNext();) {
                    SootField tokenField = (SootField) tokenFields.next();

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

                    Type replacementType = SootUtilities.createIsomorphicType(
                            localType, tokenField.getType());
                    Local replacementLocal = Jimple.v().newLocal(
                            local.getName() + "_" + tokenField.getName(),
                            replacementType);
                    body.getLocals().add(replacementLocal);
                    tokenFieldToReplacementLocal.put(tokenField,
                            replacementLocal);
                }
            }

            // 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);
                                }

View Full Code Here

Examples of soot.jimple.JimpleBody

            // What about static methods?
            if (method.isStatic()) {
                continue;
            }

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

            // Add a this local...  note that we might not have one.
            //Local thisLocal;

            try {
                /*thisLocal =*/body.getThisLocal();
            } catch (Exception ex) {
                //FIXME: what if no thisLocal?
                continue;
            }

            if (_debug) {
                System.out.println("method = " + method);
            }

            boolean doneSomething = true;

            while (doneSomething) {
                doneSomething = false;

                // System.out.println("Inlining tokens iteration " + count++);
                CompleteUnitGraph unitGraph = new CompleteUnitGraph(body);

                // This will help us figure out where locals are defined.
                _localDefs = new SimpleLocalDefs(unitGraph);

                // Analyze and tokens that are constructed in this
                // method...  these will likely not have fields
                // for them.
                _tokenAnalysis = new TokenConstructorAnalysis(body, _localDefs);

                for (Iterator units = body.getUnits().snapshotIterator(); units
                        .hasNext();) {
                    Stmt stmt = (Stmt) units.next();

                    if (!stmt.containsInvokeExpr()) {
                        continue;
View Full Code Here

Examples of soot.jimple.JimpleBody

        SootMethod initMethod = entityInstanceClass.getInitMethod();

        {
            // Populate the initialization method.
            JimpleBody body = Jimple.v().newBody(initMethod);
            initMethod.setActiveBody(body);
            body.insertIdentityStmts();

            Chain units = body.getUnits();
            Local thisLocal = body.getThisLocal();

            // Populate...
            // Initialize attributes that already exist in the class.
            ModelTransformer.createAttributes(body, entity, thisLocal, entity,
                    thisLocal, entityInstanceClass, tempCreatedMap);

            // Create and initialize ports
            ModelTransformer.createPorts(body, thisLocal, entity, thisLocal,
                    entity, entityInstanceClass, tempCreatedMap);

            // return void
            units.add(Jimple.v().newReturnVoidStmt());
        }

        // Add fields to contain the tokens for each port.
        Map nameToField = new HashMap();
        Map nameToType = new HashMap();

        {
            Iterator inputPorts = entity.inputPortList().iterator();

            while (inputPorts.hasNext()) {
                TypedIOPort port = (TypedIOPort) (inputPorts.next());
                String name = port.getName(entity);
                Type type = PtolemyUtilities.tokenType;
                nameToType.put(name, port.getType());

                SootField field = new SootField(StringUtilities
                        .sanitizeName(name)
                        + "Token", type);
                entityInstanceClass.addField(field);
                nameToField.put(name, field);
            }
        }
        // Populate the fire method.
        {
            SootMethod fireMethod = new SootMethod("fire",
                    Collections.EMPTY_LIST, VoidType.v(), Modifier.PUBLIC);
            entityInstanceClass.addMethod(fireMethod);

            JimpleBody body = Jimple.v().newBody(fireMethod);
            fireMethod.setActiveBody(body);
            body.insertIdentityStmts();

            Chain units = body.getUnits();
            Local thisLocal = body.getThisLocal();

            Local hasTokenLocal = Jimple.v().newLocal("hasTokenLocal",
                    BooleanType.v());
            body.getLocals().add(hasTokenLocal);

            Local tokenLocal = Jimple.v().newLocal("tokenLocal",
                    PtolemyUtilities.tokenType);
            body.getLocals().add(tokenLocal);

            Iterator inputPorts = entity.inputPortList().iterator();

            while (inputPorts.hasNext()) {
                TypedIOPort port = (TypedIOPort) (inputPorts.next());

                // FIXME: Handle multiports
                if (port.getWidth() > 0) {
                    String name = port.getName(entity);

                    // Create an if statement.
                    //
                    Local portLocal = Jimple.v().newLocal("port",
                            PtolemyUtilities.componentPortType);
                    body.getLocals().add(portLocal);

                    SootField portField = entityInstanceClass
                            .getFieldByName(StringUtilities.sanitizeName(name));
                    units.add(Jimple.v().newAssignStmt(
                            portLocal,
                            Jimple.v().newInstanceFieldRef(thisLocal,
                                    portField.makeRef())));
                    units.add(Jimple.v().newAssignStmt(
                            hasTokenLocal,
                            Jimple.v().newVirtualInvokeExpr(portLocal,
                                    PtolemyUtilities.hasTokenMethod.makeRef(),
                                    IntConstant.v(0))));

                    Stmt target = Jimple.v().newNopStmt();
                    units.add(Jimple.v().newIfStmt(
                            Jimple.v().newEqExpr(hasTokenLocal,
                                    IntConstant.v(0)), target));
                    units.add(Jimple.v().newAssignStmt(
                            tokenLocal,
                            Jimple.v().newVirtualInvokeExpr(portLocal,
                                    PtolemyUtilities.getMethod.makeRef(),
                                    IntConstant.v(0))));

                    SootField tokenField = entityInstanceClass
                            .getFieldByName(name + "Token");
                    units.add(Jimple.v().newAssignStmt(
                            Jimple.v().newInstanceFieldRef(thisLocal,
                                    tokenField.makeRef()), tokenLocal));
                    units.add(target);
                }
            }

            StringAttribute expressionAttribute = (StringAttribute) entity
                    .getAttribute("expression");
            String expression = expressionAttribute.getExpression();

            Local local = DataUtilities.generateExpressionCode(entity,
                    entityInstanceClass, expression, nameToField, nameToType,
                    body);

            // send the computed token
            String name = "output";
            Local portLocal = Jimple.v().newLocal("port",
                    PtolemyUtilities.componentPortType);
            body.getLocals().add(portLocal);

            SootField portField = entityInstanceClass.getFieldByName(name);

            units.add(Jimple.v().newAssignStmt(
                    portLocal,
                    Jimple.v().newInstanceFieldRef(thisLocal,
                            portField.makeRef())));
            units.add(Jimple.v().newInvokeStmt(
                    Jimple.v().newVirtualInvokeExpr(portLocal,
                            PtolemyUtilities.sendMethod.makeRef(),
                            IntConstant.v(0), local)));

            // return void
            units.add(Jimple.v().newReturnVoidStmt());

            LocalNameStandardizer.v().transform(body, "at.lns");
            LocalSplitter.v().transform(body, "at.ls");
        }

        {
            SootMethod preinitializeMethod = new SootMethod("preinitialize",
                    Collections.EMPTY_LIST, VoidType.v(), Modifier.PUBLIC);
            entityInstanceClass.addMethod(preinitializeMethod);

            JimpleBody body = Jimple.v().newBody(preinitializeMethod);
            preinitializeMethod.setActiveBody(body);
            body.insertIdentityStmts();

            Stmt insertPoint = Jimple.v().newReturnVoidStmt();
            body.getUnits().add(insertPoint);
            ModelTransformer.initializeAttributesBefore(body, insertPoint,
                    entity, body.getThisLocal(), entity, body.getThisLocal(),
                    entityInstanceClass);
            LocalNameStandardizer.v().transform(body, "at.lns");
            LocalSplitter.v().transform(body, "at.ls");
        }
View Full Code Here

Examples of soot.jimple.JimpleBody

            _errorObject.setName("Error");
        } catch (Exception ex) {
            // Ignore..
        }

        JimpleBody body = (JimpleBody) method.getActiveBody();
        _localToObject = new HashMap();

        if (method.isStatic()) {
            //           System.out.println("Ignoring this binding for static method: "
            //                     + method);
        } else {
            _set(body.getThisLocal(), thisBinding);
        }

        _notDone = true;

        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

Examples of soot.jimple.JimpleBody

            for (Iterator methods = theClass.getMethods().iterator(); methods
                    .hasNext();) {
                SootMethod method = (SootMethod) methods.next();

                // System.out.println("method = " + method);
                JimpleBody body = (JimpleBody) method.retrieveActiveBody();

                for (Iterator units = body.getUnits().snapshotIterator(); units
                        .hasNext();) {
                    Stmt unit = (Stmt) units.next();

                    if (unit.containsFieldRef()) {
                        ValueBox box = unit.getFieldRefBox();
                        Value value = box.getValue();

                        if (value instanceof InstanceFieldRef) {
                            InstanceFieldRef fieldRef = (InstanceFieldRef) value;
                            SootField field = fieldRef.getField();

                            // Turn off debugging..
                            if (field.getSubSignature().equals(
                                    PtolemyUtilities.debuggingField
                                            .getSubSignature())) {
                                if (unit instanceof AssignStmt) {
                                    body.getUnits().insertBefore(
                                            Jimple.v().newAssignStmt(
                                                    ((AssignStmt) unit)
                                                            .getLeftOp(),
                                                    IntConstant.v(0)), unit);
                                }

                                body.getUnits().remove(unit);
                            } else if (field.getSubSignature().equals(
                                    PtolemyUtilities.stopRequestedField
                                            .getSubSignature())) {
                                // Assume stops are not requested..
                                if (unit instanceof AssignStmt
                                        && (box == ((AssignStmt) unit)
                                                .getLeftOpBox())) {
                                    body.getUnits().remove(unit);
                                } else {
                                    box.setValue(IntConstant.v(0));
                                }
                            }
                        }
                    } else if (unit.containsInvokeExpr()) {
                        ValueBox box = unit.getInvokeExprBox();
                        Value value = box.getValue();

                        if ((value instanceof InvokeExpr)
                                && !(value instanceof SpecialInvokeExpr)) {
                            // remove attachText
                            InvokeExpr expr = (InvokeExpr) value;

                            if (expr.getMethod().getSubSignature().equals(
                                    PtolemyUtilities.attachTextMethod
                                            .getSubSignature())) {
                                body.getUnits().remove(unit);
                            } else if (expr.getMethod().getSubSignature()
                                    .equals(
                                            PtolemyUtilities.setNameMethod
                                                    .getSubSignature())) {
                                body.getUnits().remove(unit);
                            } else if (expr.getMethod().getName().equals(
                                    "_debug")) {
                                body.getUnits().remove(unit);
                                //                             } else if (expr.getMethod().getName().equals(
                                //                                     "initialize")) {
                                //                                 // Assume no Initializables
                                //                                 body.getUnits().remove(unit);
                                //                             } else if (expr.getMethod().getName().equals(
                                //                                     "preinitialize")) {
                                //                                 // Assume no Initializables
                                //                                 body.getUnits().remove(unit);
                                //                             } else if (expr.getMethod().getName().equals(
                                //                                     "wrapup")) {
                                //                                 // Assume no Initializables
                                //                                 body.getUnits().remove(unit);
                            }

                            // Inline namedObj methods on the
                            // attribute.
                            // FIXME: This should do the
                            // whole traceback business to ensure that
                            // we are calling the methods on the
                            // toplevel object.  This assumes we've
                            // already removed other namedobj methods
                            // on the object objects already.  See
                            // InlineParameterTransformer and
                            // InlinePortTransformer
                            if (expr.getMethod().getSubSignature().equals(
                                    PtolemyUtilities.getFullNameMethod
                                            .getSubSignature())) {
                                if (unit instanceof AssignStmt) {
                                    body.getUnits().insertBefore(
                                            Jimple.v().newAssignStmt(
                                                    ((AssignStmt) unit)
                                                            .getLeftOp(),
                                                    StringConstant.v(_model
                                                            .getFullName())),
                                            unit);
                                }

                                body.getUnits().remove(unit);
                            } else if (expr.getMethod().getSubSignature()
                                    .equals(
                                            PtolemyUtilities.getNameMethod
                                                    .getSubSignature())) {
                                if (unit instanceof AssignStmt) {
                                    body.getUnits().insertBefore(
                                            Jimple.v().newAssignStmt(
                                                    ((AssignStmt) unit)
                                                            .getLeftOp(),
                                                    StringConstant.v(_model
                                                            .getName())), unit);
                                }

                                body.getUnits().remove(unit);
                            } else if (expr.getMethod().getSubSignature()
                                    .equals(
                                            PtolemyUtilities.findEffigyMethod
                                                    .getSubSignature())) {
                                if (unit instanceof AssignStmt) {
                                    body.getUnits().insertBefore(
                                            Jimple.v().newAssignStmt(
                                                    ((AssignStmt) unit)
                                                            .getLeftOp(),
                                                    NullConstant.v()), unit);
                                }

                                body.getUnits().remove(unit);
                            } else if (expr.getMethod().getSubSignature()
                                    .equals(
                                            PtolemyUtilities.getModelURIMethod
                                                    .getSubSignature())) {
                                if (unit instanceof AssignStmt) {
                                    SootClass uriClass = Scene
                                            .v()
                                            .loadClassAndSupport("java.net.URI");
                                    RefType type = RefType.v(uriClass);
                                    SootMethod initMethod = uriClass
                                            .getMethod("void <init>(java.lang.String)");
                                    Local local = (Local) ((AssignStmt) unit)
                                            .getLeftOp();
                                    String uriString = URIAttribute
                                            .getModelURI(_model).toString();
                                    body
                                            .getUnits()
                                            .insertBefore(
                                                    Jimple
                                                            .v()
                                                            .newAssignStmt(
                                                                    local,
                                                                    Jimple
                                                                            .v()
                                                                            .newNewExpr(
                                                                                    type)),
                                                    unit);
                                    body
                                            .getUnits()
                                            .insertBefore(
                                                    Jimple
                                                            .v()
                                                            .newInvokeStmt(
                                                                    Jimple
                                                                            .v()
                                                                            .newSpecialInvokeExpr(
                                                                                    local,
                                                                                    initMethod
                                                                                            .makeRef(),
                                                                                    StringConstant
                                                                                            .v(uriString))),
                                                    unit);
                                }

                                body.getUnits().remove(unit);
                            } else if (expr
                                    .getMethod()
                                    .getSubSignature()
                                    .equals(
                                            PtolemyUtilities.handleModelErrorMethod
                                                    .getSubSignature())) {
                                // Replace handleModelError with a throw clause.
                                body.getUnits()
                                        .insertBefore(
                                                Jimple.v().newThrowStmt(
                                                        expr.getArg(1)), unit);
                                body.getUnits().remove(unit);
                            }
                        }
                    }
                }
            }
        }

        List modifiedConstructorClassList = new LinkedList();

        // Loop over all the classes
        for (Iterator i = Scene.v().getApplicationClasses().iterator(); i
                .hasNext();) {
            SootClass theClass = (SootClass) i.next();

            if (SootUtilities
                    .derivesFrom(theClass, PtolemyUtilities.actorClass)
                    || SootUtilities.derivesFrom(theClass,
                            PtolemyUtilities.compositeActorClass)
                    || SootUtilities.derivesFrom(theClass,
                            PtolemyUtilities.attributeClass)) {
                System.out.println("changing superclass for " + theClass);
                theClass.setSuperclass(PtolemyUtilities.objectClass);

                // Fix the constructor for the actor to take just a
                // container argument, if it was a 2 arg constructor,
                // or no arguments otherwise.  FIXME: Here we assume
                // that there is just one constructor.. This will
                // throw an exception if there is more than one, in
                // which case we need to improve this code.
                SootMethod method = null;

                try {
                    method = theClass.getMethodByName("<init>");
                } catch (RuntimeException ex) {
                    System.out.println("Could not get method <init> by name "
                            + "from class " + theClass);
                    System.out.println("Methods = " + theClass.getMethods());
                    throw ex;
                }

                if (method.getParameterCount() == 2) {
                    // Change the constructor so that it only takes the container.
                    SootField containerField = theClass
                            .getFieldByName(ModelTransformer
                                    .getContainerFieldName());
                    RefType containerType = (RefType) containerField.getType();
                    List typeList = new LinkedList();
                    typeList.add(containerType);
                    method.setParameterTypes(typeList);
                } else {
                    method.setParameterTypes(Collections.EMPTY_LIST);
                }

                // Keep track of the modification, so we know to
                // modify invocations of that constructor.
                modifiedConstructorClassList.add(theClass);

                // Dance so that indexes in the Scene are properly updated.
                theClass.removeMethod(method);
                theClass.addMethod(method);

                System.out.println("method = " + method);
                JimpleBody body = (JimpleBody) method.retrieveActiveBody();

                for (Iterator units = body.getUnits().snapshotIterator(); units
                        .hasNext();) {
                    Stmt unit = (Stmt) units.next();

                    if (unit.containsInvokeExpr()) {
                        ValueBox box = unit.getInvokeExprBox();
                        Value value = box.getValue();

                        if (value instanceof SpecialInvokeExpr) {
                            // Fix super.<init> calls. constructor...
                            SpecialInvokeExpr expr = (SpecialInvokeExpr) value;

                            if (expr.getBase().equals(body.getThisLocal())
                                    && expr.getMethodRef().name().equals(
                                            "<init>")) {
                                //             System.out.println("replacing constructor = "
                                //       + unit + " in method " + method);
                                // Replace with zero arg object constructor.
                                box.setValue(Jimple.v().newSpecialInvokeExpr(
                                        (Local) expr.getBase(),
                                        PtolemyUtilities.objectConstructor
                                                .makeRef(),
                                        Collections.EMPTY_LIST));
                            }
                        }
                    } else if (unit instanceof IdentityStmt) {
                        IdentityStmt identityStmt = (IdentityStmt) unit;
                        Value value = identityStmt.getRightOp();

                        if (value instanceof ParameterRef) {
                            ParameterRef parameterRef = (ParameterRef) value;

                            if ((parameterRef.getIndex() == 0)
                                    && (method.getParameterCount() == 1)) {
                                //       System.out.println("found = " + identityStmt);
                                ValueBox box = identityStmt.getRightOpBox();
                                box.setValue(Jimple.v().newParameterRef(
                                        method.getParameterType(0), 0));

                                //    System.out.println("changed to: " + identityStmt);
                            } else {
                                // Parameter values are null.  Note that
                                // we need to make sure that the
                                // assignment to null happens after all
                                // the identity statements, otherwise the
                                // super constructor will be implicitly
                                // called.
                                body.getUnits().remove(identityStmt);
                                body.getUnits().insertBefore(
                                        Jimple.v().newAssignStmt(
                                                identityStmt.getLeftOp(),
                                                NullConstant.v()),
                                        body.getFirstNonIdentityStmt());
                            }
                        } //  else if (value instanceof ThisRef) {

                        //                             // Fix the type of thisRefs.
                        //                             ValueBox box = identityStmt.getRightOpBox();
                        //                             box.setValue(
                        //                                     Jimple.v().newThisRef(
                        //                                             RefType.v(PtolemyUtilities.objectClass)));
                        //                         }
                    }
                }
            }
        }

        // Reset the hierarchy, since we've changed superclasses and such.
        Scene.v().setActiveHierarchy(new Hierarchy());
        Scene.v().setFastHierarchy(new FastHierarchy());

        // Fix the specialInvokes.
        for (Iterator i = Scene.v().getApplicationClasses().iterator(); i
                .hasNext();) {
            SootClass theClass = (SootClass) i.next();

            // Loop through all the methods in the class.
            for (Iterator methods = theClass.getMethods().iterator(); methods
                    .hasNext();) {
                SootMethod method = (SootMethod) methods.next();

                // System.out.println("method = " + method);
                JimpleBody body = (JimpleBody) method.retrieveActiveBody();

                for (Iterator units = body.getUnits().snapshotIterator(); units
                        .hasNext();) {
                    Stmt unit = (Stmt) units.next();

                    if (unit.containsInvokeExpr()) {
                        ValueBox box = unit.getInvokeExprBox();
                        Value value = box.getValue();

                        if (value instanceof SpecialInvokeExpr) {
                            // If we're constructing one of our actor classes,
                            // then switch to the modified constructor.
                            SpecialInvokeExpr expr = (SpecialInvokeExpr) value;
                            SootClass declaringClass = expr.getMethodRef()
                                    .declaringClass();

                            if (expr.getMethodRef().name().equals("<init>")
                                    && modifiedConstructorClassList
                                            .contains(declaringClass)) {
                                System.out
                                        .println("replacing constructor invocation = "
                                                + unit + " in method " + method);
                                SootMethod newConstructor = declaringClass
                                        .getMethodByName("<init>");

                                if (newConstructor.getParameterCount() == 1) {
                                    // Replace with just container arg constructor.
                                    List args = new LinkedList();
                                    args.add(expr.getArg(0));
                                    box.setValue(Jimple.v()
                                            .newSpecialInvokeExpr(
                                                    (Local) expr.getBase(),
                                                    newConstructor.makeRef(),
                                                    args));
                                } else {
                                    // Replace with zero arg constructor.
                                    box.setValue(Jimple.v()
                                            .newSpecialInvokeExpr(
                                                    (Local) expr.getBase(),
                                                    newConstructor.makeRef(),
                                                    Collections.EMPTY_LIST));
                                }
                            }
                        }
                    }
                }
            }
        }

        for (Iterator i = Scene.v().getApplicationClasses().iterator(); i
                .hasNext();) {
            SootClass theClass = (SootClass) i.next();

            // Loop through all the methods in the class.
            for (Iterator methods = theClass.getMethods().iterator(); methods
                    .hasNext();) {
                SootMethod method = (SootMethod) methods.next();

                // System.out.println("method = " + method);
                JimpleBody body = (JimpleBody) method.retrieveActiveBody();

                // Infer types.
                LocalSplitter.v().transform(body, "nee.ls");
                TypeAssigner.v().transform(body, "nee.ta");
            }
        }

        for (Iterator i = Scene.v().getApplicationClasses().iterator(); i
                .hasNext();) {
            SootClass theClass = (SootClass) i.next();

            // Loop through all the methods in the class.
            for (Iterator methods = theClass.getMethods().iterator(); methods
                    .hasNext();) {
                SootMethod method = (SootMethod) methods.next();

                // System.out.println("method = " + method);
                JimpleBody body = (JimpleBody) method.retrieveActiveBody();
                for (Iterator units = body.getUnits().snapshotIterator(); units
                        .hasNext();) {
                    Stmt unit = (Stmt) units.next();

                    //        System.out.println("unit = " + unit);
                    // If any box is removable, then remove the statement.
                    for (Iterator boxes = unit.getUseAndDefBoxes().iterator(); boxes
                            .hasNext();) {
                        ValueBox box = (ValueBox) boxes.next();

                        Value value = box.getValue();
                        Type type = value.getType();

                        if (_isRemovableType(type)) {
                            //  System.out.println("Unit with removable type "
                            //        + type + ": " + unit);
                            body.getUnits().remove(unit);
                            break;
                        }
                    }

                    // If any locals are removable, then remove them.
                    for (Iterator locals = body.getLocals().snapshotIterator(); locals
                            .hasNext();) {
                        Local local = (Local) locals.next();
                        Type type = local.getType();

                        if (_isRemovableType(type)) {
                            body.getLocals().remove(local);
                        }
                    }
                }

                // If any fields are removable, then remove them.
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.