Examples of JimpleBody


Examples of soot.jimple.JimpleBody

     @param tracker The ExceptionTracker.
     *  @param method  The method for which labels need to be initialized.
     */
    protected void _initializeLabels(CSwitch visitor, ExceptionTracker tracker,
            SootMethod method) {
        JimpleBody body = (JimpleBody) method.retrieveActiveBody();
        Iterator units = body.getUnits().iterator();

        while (units.hasNext()) {
            Unit unit = (Unit) (units.next());
            Unit target = null;

View Full Code Here

Examples of soot.jimple.JimpleBody

            SootMethod newMethod = new SootMethod(oldMethod.getName(),
                    oldMethod.getParameterTypes(), oldMethod.getReturnType(),
                    oldMethod.getModifiers(), oldMethod.getExceptions());
            theClass.addMethod(newMethod);

            JimpleBody newBody = Jimple.v().newBody(newMethod);
            newMethod.setActiveBody(newBody);
            newBody.insertIdentityStmts();

            //System.out.println("method = " + newMethod);
            //System.out.println("oldMethod = " + oldMethod);
            // Call the super method
            Chain units = newBody.getUnits();

            // get a list of the locals that reference
            // the parameters of the
            // constructor.
            List parameterList = new ArrayList();
            parameterList.addAll(newBody.getLocals());

            Stmt invokeStmt = null;

            // Invoke the method...
            // handling static and void methods differently
            if (oldMethod.getReturnType() == VoidType.v()) {
                InvokeExpr invokeExpr;

                if (newMethod.isStatic()) {
                    invokeExpr = Jimple.v().newStaticInvokeExpr(
                            oldMethod.makeRef(), parameterList);
                } else {
                    Local thisLocal = newBody.getThisLocal();
                    parameterList.remove(thisLocal);
                    invokeExpr = Jimple.v().newVirtualInvokeExpr(thisLocal,
                            oldMethod.makeRef(), parameterList);
                }

                invokeStmt = Jimple.v().newInvokeStmt(invokeExpr);
                units.add(invokeStmt);

                // return void
                units.add(Jimple.v().newReturnVoidStmt());
            } else {
                InvokeExpr invokeExpr;

                // Create a new local for the return value.
                Local returnValueLocal = Jimple.v().newLocal("returnValue",
                        oldMethod.getReturnType());
                newBody.getLocals().add(returnValueLocal);

                if (newMethod.isStatic()) {
                    invokeExpr = Jimple.v().newStaticInvokeExpr(
                            oldMethod.makeRef(), parameterList);
                } else {
                    Local thisLocal = newBody.getThisLocal();
                    parameterList.remove(thisLocal);
                    invokeExpr = Jimple.v().newVirtualInvokeExpr(thisLocal,
                            oldMethod.makeRef(), parameterList);
                }

                invokeStmt = Jimple.v().newAssignStmt(returnValueLocal,
                        invokeExpr);
                units.add(invokeStmt);

                // return the value
                units.add(Jimple.v().newReturnStmt(returnValueLocal));
            }
        }

        // Loop through all the methods again, this time looking for
        // method invocations on the old superClass...  Inline these calls.
        // This code is similar to inlineCallsToMethod, but avoids iterating
        // over all the methods in the class twice, which could get
        // very expensive.
        for (Iterator methods = theClass.getMethods().iterator(); methods
                .hasNext();) {
            SootMethod newMethod = (SootMethod) methods.next();
            Body newBody = newMethod.retrieveActiveBody();

            // use a snapshotIterator since we are going to be manipulating
            // the statements.
            Iterator j = newBody.getUnits().snapshotIterator();

            while (j.hasNext()) {
                Stmt stmt = (Stmt) j.next();

                System.out.println("stmt = " + stmt);
View Full Code Here

Examples of soot.jimple.JimpleBody

     *  in the given method.
     *  @return true if any changes were made.
     */
    public static boolean inlineCallsOnThisInMethod(SootMethod method) {
        SootClass theClass = method.getDeclaringClass();
        JimpleBody body = (JimpleBody) method.retrieveActiveBody();

        // use a snapshotIterator since we are going to be manipulating
        // the statements.
        boolean inlinedAnything = false;
        Iterator j = body.getUnits().snapshotIterator();

        while (j.hasNext()) {
            Stmt stmt = (Stmt) j.next();

            if (stmt.containsInvokeExpr()) {
                InvokeExpr invoke = stmt.getInvokeExpr();

                if (method instanceof StaticInvokeExpr) {
                    // simply inline static methods.
                    SootMethod inlineMethod = invoke.getMethod();

                    // Don't inline a recursive method call.
                    if (inlineMethod.equals(method)) {
                        continue;
                    }

                    SiteInliner.inlineSite(inlineMethod, stmt, method);
                    inlinedAnything = true;
                } else if (!method.isStatic()
                        && invoke instanceof InstanceInvokeExpr) {
                    InstanceInvokeExpr instanceInvoke = (InstanceInvokeExpr) invoke;

                    if (instanceInvoke.getBase().equals(body.getThisLocal())) {
                        SootMethod inlineMethod;

                        if (instanceInvoke instanceof VirtualInvokeExpr) {
                            inlineMethod = searchForMethodByName(theClass,
                                    method.getName());
View Full Code Here

Examples of soot.jimple.JimpleBody

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

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

            for (Iterator useBoxes = body.getUseAndDefBoxes().iterator(); useBoxes
                    .hasNext();) {
                ValueBox box = (ValueBox) useBoxes.next();

                if (box.getValue() instanceof InstanceFieldRef) {
                    InstanceFieldRef expr = (InstanceFieldRef) box.getValue();
View Full Code Here

Examples of soot.jimple.JimpleBody

                .getMethod("boolean hasNext()");

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

            BlockGraph graph = new CompleteBlockGraph(body);

            for (Iterator blocks = graph.iterator(); blocks.hasNext();) {
                Block block = (Block) blocks.next();

                // System.out.println("body = " + block);
                // filter out anything that doesn't look like a loop body.
                if ((block.getPreds().size() != 1)
                        || (block.getSuccs().size() != 1)) {
                    continue;
                }

                // filter out anything that isn't attached to something
                // that looks like a conditional jump.
                Block whileCond = (Block) block.getSuccs().get(0);

                //  System.out.println("cond = " + whileCond);
                if ((whileCond != block.getPreds().get(0))
                        || (whileCond.getPreds().size() != 2)
                        || (whileCond.getSuccs().size() != 2)) {
                    continue;
                }

                // filter out anything that doesn't start with a call
                // to hasNext().
                if (!(whileCond.getHead() instanceof DefinitionStmt)) {
                    continue;
                }

                DefinitionStmt stmt = (DefinitionStmt) whileCond.getHead();

                if (!(stmt.getRightOp() instanceof InterfaceInvokeExpr)) {
                    continue;
                }

                InterfaceInvokeExpr expr = (InterfaceInvokeExpr) stmt
                        .getRightOp();

                if (expr.getMethod() != iteratorHasNextMethod) {
                    continue;
                }

                // At this point we know we have a while (hasNext()) loop.
                // Now go check for iterator is defined...  it should be just
                // above
                Local iteratorLocal = (Local) expr.getBase();
                Block whilePredecessor = (Block) whileCond.getPreds().get(0);

                if (whilePredecessor == block) {
                    whilePredecessor = (Block) whileCond.getPreds().get(1);
                }

                // System.out.println("whilePredecessor = " + whilePredecessor);
                Unit unit = whilePredecessor.getTail();
                boolean found = false;

                // walk backwards until we find a definition of the iterator.
                while ((unit != whilePredecessor.getHead()) && !found) {
                    if (unit instanceof DefinitionStmt
                            && ((DefinitionStmt) unit).getLeftOp().equals(
                                    iteratorLocal)) {
                        found = true;
                    } else {
                        unit = whilePredecessor.getPredOf(unit);
                    }
                }

                //  System.out.println("iterator def = " + unit);
                DefinitionStmt iteratorDefinition = ((DefinitionStmt) unit);

                if (!(iteratorDefinition.getRightOp() instanceof InterfaceInvokeExpr)
                        || !((InterfaceInvokeExpr) iteratorDefinition
                                .getRightOp()).getMethod().getName().equals(
                                "iterator")) {
                    continue;
                }

                Local collectionLocal = (Local) ((InterfaceInvokeExpr) iteratorDefinition
                        .getRightOp()).getBase();

                //  System.out.println("collection Local = " + collectionLocal);
                found = false;

                // Walk backward again until we reach the definition
                // of the collection.
                while ((unit != whilePredecessor.getHead()) && !found) {
                    if (unit instanceof DefinitionStmt
                            && ((DefinitionStmt) unit).getLeftOp().equals(
                                    collectionLocal)) {
                        found = true;
                    } else {
                        unit = whilePredecessor.getPredOf(unit);
                    }
                }

                //  System.out.println("collection def = " + unit);
                // System.out.println("field = " + field);
                DefinitionStmt collectionDefinition = ((DefinitionStmt) unit);

                if (!(collectionDefinition.getRightOp() instanceof FieldRef)
                        || (((FieldRef) collectionDefinition.getRightOp())
                                .getField() != field)) {
                    continue;
                }

                // FINALLY we know we've found something we can unroll... :)
                // System.out.println("is unrollable...");
                // There should be a jump from the predecessor to the
                // condition.  Redirect this jump to the body.
                whileCond.getHead().redirectJumpsToThisTo(block.getHead());

                Local thisLocal = body.getThisLocal();
                Chain units = body.getUnits();
                List blockStmtList = new LinkedList();

                // pull the statements that we are inlining out of the block
                // so that we can copy them.  Note that this also removes
                // them from the method body.
                Unit insertPoint = (Unit) units.getSuccOf(block.getTail());

                for (Iterator blockStmts = block.iterator(); blockStmts
                        .hasNext();) {
                    Stmt original = (Stmt) blockStmts.next();
                    blockStmtList.add(original);
                    blockStmts.remove();
                }

                // Remove the jump that should be the final statement.
                blockStmtList.remove(blockStmtList
                        .get(blockStmtList.size() - 1));

                // Loop through and unroll the loop body once for
                // every element of the field list.
                for (Iterator fields = fieldList.iterator(); fields.hasNext();) {
                    SootField insertField = (SootField) fields.next();

                    for (Iterator blockStmts = blockStmtList.iterator(); blockStmts
                            .hasNext();) {
                        // clone each statement
                        Stmt original = (Stmt) blockStmts.next();
                        Stmt clone = (Stmt) original.clone();

                        // If the statement is a call to the next() method,
                        // then inline it with the next value of the iterator.
                        for (Iterator boxes = clone.getUseBoxes().iterator(); boxes
                                .hasNext();) {
                            ValueBox box = (ValueBox) boxes.next();
                            Value value = box.getValue();

                            if (value instanceof InvokeExpr) {
                                InvokeExpr r = (InvokeExpr) value;

                                if (r.getMethod() == iteratorNextMethod) {
                                    box.setValue(Jimple.v()
                                            .newInstanceFieldRef(thisLocal,
                                                    insertField.makeRef()));
                                }
                            }
                        }

                        units.insertBefore(clone, insertPoint);
                    }
                }

                // remove the conditional
                for (Iterator blockStmts = whileCond.iterator(); blockStmts
                        .hasNext();) {
                    /*Stmt original = (Stmt)*/blockStmts.next();
                    blockStmts.remove();
                }

                body.getUnits().remove(iteratorDefinition);

                // Find while loops.
                // This code modified from WhileMatcher.

                /*
 
View Full Code Here

Examples of soot.jimple.JimpleBody

                    getAttributeComputationFunctionName(attribute, context),
                    Collections.EMPTY_LIST, VoidType.v(), Modifier.PUBLIC);
            Type variableType = RefType.v(PtolemyUtilities.variableClass);
            theClass.addMethod(method);

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

            Stmt insertPoint = Jimple.v().newReturnVoidStmt();
            body.getUnits().add(insertPoint);

            if (_constAnalysis.getConstVariables(namedObj).contains(attribute)) {
                Local local = SootUtilities.createRuntimeException(body,
                        insertPoint,
                        "Parameter is constant and should not be re-evaluated");
                body.getUnits().insertBefore(Jimple.v().newThrowStmt(local),
                        insertPoint);
                continue;
            }

            // A local that we will use to set the value of our
            // settable attributes.
            Local attributeLocal = Jimple.v().newLocal("attribute",
                    PtolemyUtilities.attributeType);
            body.getLocals().add(attributeLocal);

            Local settableLocal = Jimple.v().newLocal("settable",
                    PtolemyUtilities.settableType);
            body.getLocals().add(settableLocal);

            //String className = attribute.getClass().getName();
            //Type attributeType = RefType.v(className);
            String attributeName = attribute.getName(context);
            //String fieldName = getFieldNameForAttribute(attribute, context);

            Local local = attributeLocal;
            body.getUnits().insertBefore(
                    Jimple.v().newAssignStmt(
                            attributeLocal,
                            Jimple.v().newVirtualInvokeExpr(
                                    body.getThisLocal(),
                                    PtolemyUtilities.getAttributeMethod
                                            .makeRef(),
                                    StringConstant.v(attributeName))),
                    insertPoint);

            if (attribute instanceof Variable) {
                // If the attribute is a parameter, then set its
                // token to the correct value.
                //                 Token token = null;
                //                 try {
                //                     token = ((Variable)attribute).getToken();
                //                 } catch (IllegalActionException ex) {
                //                     throw new RuntimeException(ex.getMessage());
                //                 }
                //                 if (token == null) {
                //                     throw new RuntimeException("Calling getToken() on '"
                //                             + attribute + "' returned null.  This may occur "
                //                             + "if an attribute has no value in the moml file");
                //                 }
                //                 Local tokenLocal =
                //                     PtolemyUtilities.buildConstantTokenLocal(body,
                //                             insertPoint, token, "token");
                Local tokenLocal = DataUtilities.generateExpressionCodeBefore(
                        (Entity) context, theClass, ((Variable) attribute)
                                .getExpression(), new HashMap(), new HashMap(),
                        body, insertPoint);

                Local variableLocal = Jimple.v().newLocal("variable",
                        variableType);
                body.getLocals().add(variableLocal);

                // cast to Variable.
                body.getUnits().insertBefore(
                        Jimple.v().newAssignStmt(variableLocal,
                                Jimple.v().newCastExpr(local, variableType)),
                        insertPoint);

                // call setToken.
                body.getUnits().insertBefore(
                        Jimple.v().newInvokeStmt(
                                Jimple.v().newVirtualInvokeExpr(
                                        variableLocal,
                                        PtolemyUtilities.variableSetTokenMethod
                                                .makeRef(), tokenLocal)),
                        insertPoint);

                body.getUnits().insertBefore(
                        Jimple.v().newInvokeStmt(
                                Jimple.v().newInterfaceInvokeExpr(
                                        variableLocal,
                                        PtolemyUtilities.validateMethod
                                                .makeRef())), insertPoint);
            } else if (attribute instanceof Settable) {
                // If the attribute is settable, then set its
                // expression.
                // cast to Settable.
                body.getUnits().insertBefore(
                        Jimple.v().newAssignStmt(settableLocal, local),
                        insertPoint);

                String expression = ((Settable) attribute).getExpression();

                // call setExpression.
                body.getUnits().insertBefore(
                        Jimple.v().newInvokeStmt(
                                Jimple.v().newInterfaceInvokeExpr(
                                        settableLocal,
                                        PtolemyUtilities.setExpressionMethod
                                                .makeRef(),
                                        StringConstant.v(expression))),
                        insertPoint);

                // call validate to ensure that attributeChanged is called.
                body.getUnits().insertBefore(
                        Jimple.v().newInvokeStmt(
                                Jimple.v().newInterfaceInvokeExpr(
                                        settableLocal,
                                        PtolemyUtilities.validateMethod
                                                .makeRef())), insertPoint);
View Full Code Here

Examples of soot.jimple.JimpleBody

    public static void implementExecutableInterface(SootClass theClass) {
        // Loop through all the methods and remove calls to super.
        for (Iterator methods = theClass.getMethods().iterator(); methods
                .hasNext();) {
            SootMethod method = (SootMethod) methods.next();
            JimpleBody body = (JimpleBody) method.retrieveActiveBody();

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

                if (!stmt.containsInvokeExpr()) {
                    continue;
                }

                ValueBox box = stmt.getInvokeExprBox();
                Value value = box.getValue();

                if (value instanceof SpecialInvokeExpr) {
                    SpecialInvokeExpr r = (SpecialInvokeExpr) value;

                    if (PtolemyUtilities.executableInterface.declaresMethod(r
                            .getMethod().getSubSignature())) {
                        boolean isNonVoidMethod = r.getMethod().getName()
                                .equals("prefire")
                                || r.getMethod().getName().equals("postfire");

                        if (isNonVoidMethod && stmt instanceof AssignStmt) {
                            box.setValue(IntConstant.v(1));
                        } else {
                            System.out.println("mt: executable: removing "
                                    + stmt);
                            body.getUnits().remove(stmt);
                        }
                    }
                    if (PtolemyUtilities.initializableInterface
                            .declaresMethod(r.getMethod().getSubSignature())) {
                        System.out.println("mt: initializable: removing "
                                + stmt);
                        body.getUnits().remove(stmt);
                    }
                }
            }
        }

        // The initialize method implemented in the actor package is weird,
        // because it calls getDirector.  Since we don't need it,
        // make sure that we never call the baseclass initialize method.
        if (!theClass.declaresMethodByName("preinitialize")) {
            SootMethod method = new SootMethod("preinitialize",
                    Collections.EMPTY_LIST, VoidType.v(), Modifier.PUBLIC);
            theClass.addMethod(method);

            JimpleBody body = Jimple.v().newBody(method);
            method.setActiveBody(body);
            body.insertIdentityStmts();
            body.getUnits().add(Jimple.v().newReturnVoidStmt());
        }

        if (!theClass.declaresMethodByName("initialize")) {
            SootMethod method = new SootMethod("initialize",
                    Collections.EMPTY_LIST, VoidType.v(), Modifier.PUBLIC);
            theClass.addMethod(method);

            JimpleBody body = Jimple.v().newBody(method);
            method.setActiveBody(body);
            body.insertIdentityStmts();
            body.getUnits().add(Jimple.v().newReturnVoidStmt());
        }

        if (!theClass.declaresMethodByName("prefire")) {
            SootMethod method = new SootMethod("prefire",
                    Collections.EMPTY_LIST, BooleanType.v(), Modifier.PUBLIC);
            theClass.addMethod(method);

            JimpleBody body = Jimple.v().newBody(method);
            method.setActiveBody(body);
            body.insertIdentityStmts();
            body.getUnits().add(Jimple.v().newReturnStmt(IntConstant.v(1)));
        }

        if (!theClass.declaresMethodByName("fire")) {
            SootMethod method = new SootMethod("fire", Collections.EMPTY_LIST,
                    VoidType.v(), Modifier.PUBLIC);
            theClass.addMethod(method);

            JimpleBody body = Jimple.v().newBody(method);
            method.setActiveBody(body);
            body.insertIdentityStmts();
            body.getUnits().add(Jimple.v().newReturnVoidStmt());
        }

        if (!theClass.declaresMethodByName("postfire")) {
            SootMethod method = new SootMethod("postfire",
                    Collections.EMPTY_LIST, BooleanType.v(), Modifier.PUBLIC);
            theClass.addMethod(method);

            JimpleBody body = Jimple.v().newBody(method);
            method.setActiveBody(body);
            body.insertIdentityStmts();
            body.getUnits().add(Jimple.v().newReturnStmt(IntConstant.v(1)));
        }

        if (!theClass.declaresMethodByName("wrapup")) {
            SootMethod method = new SootMethod("wrapup",
                    Collections.EMPTY_LIST, VoidType.v(), Modifier.PUBLIC);
            theClass.addMethod(method);

            JimpleBody body = Jimple.v().newBody(method);
            method.setActiveBody(body);
            body.insertIdentityStmts();
            body.getUnits().add(Jimple.v().newReturnVoidStmt());
        }
    }
View Full Code Here

Examples of soot.jimple.JimpleBody

                continue;
            }

            // create the new constructor.
            SootMethod constructor = _createConstructor(this, method);
            JimpleBody body = (JimpleBody) constructor.getActiveBody();
            Chain units = body.getUnits();
            Local thisLocal = body.getThisLocal();

            // Call the __CGInit method.
            units.add(Jimple.v().newInvokeStmt(
                    Jimple.v().newVirtualInvokeExpr(thisLocal,
                            _initMethod.makeRef())));
View Full Code Here

Examples of soot.jimple.JimpleBody

        theClass.addMethod(constructor);

        // System.out.println("creating constructor = " +
        //        constructor.getSignature());
        // create empty body
        JimpleBody body = Jimple.v().newBody(constructor);

        // Add this and read the parameters into locals
        body.insertIdentityStmts();
        constructor.setActiveBody(body);

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

        // get a list of the locals that reference the parameters of the
        // constructor.  What a nice hack.
        List parameterList = new ArrayList();
        parameterList.addAll(body.getLocals());
        parameterList.remove(thisLocal);

        // Call the super constructor.
        units.add(Jimple.v().newInvokeStmt(
                Jimple.v().newSpecialInvokeExpr(thisLocal,
View Full Code Here

Examples of soot.jimple.JimpleBody

        // FIXME: what if the inlined code contains another call
        // to this class???
        for (Iterator methods = theClass.getMethods().iterator(); methods
                .hasNext();) {
            SootMethod method = (SootMethod) methods.next();
            JimpleBody body = (JimpleBody) method.retrieveActiveBody();

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

                if (!stmt.containsInvokeExpr()) {
                    continue;
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.