Package soot

Examples of soot.ValueBox


            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();

                    if (expr.getField() == field) {
                        System.out.println("fixing field = " + expr);
                        box.setValue(Jimple.v().newStaticFieldRef(
                                expr.getField().makeRef()));
                    }
                }
            }
        }
View Full Code Here


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

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

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

                    // Replace Array creations with a more specific
                    // type, if possible.
                    if (box.getValue() instanceof NewArrayExpr) {
                        NewArrayExpr newArrayExpr = (NewArrayExpr) box
                                .getValue();

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

                        Type baseType = newArrayExpr.getBaseType();
                        Type newType = typeAnalysis
                                .getSpecializedSootType(newArrayExpr);

                        if ((newType != null) && !newType.equals(baseType)) {
                            if (debug) {
                                System.out.println("replacing with " + newType);
                            }

                            box.setValue(Jimple.v().newNewArrayExpr(newType,
                                    newArrayExpr.getSize()));
                        }
                    }
                }

View Full Code Here

                .hasNext();) {
            Stmt stmt = (Stmt) units.next();

            for (Iterator boxes = stmt.getUseBoxes().iterator(); boxes
                    .hasNext();) {
                ValueBox box = (ValueBox) boxes.next();
                Value value = box.getValue();

                if (value instanceof BinopExpr) {
                    BinopExpr binop = (BinopExpr) value;
                    Value left = binop.getOp1();
                    Value right = binop.getOp2();
View Full Code Here

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

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

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

                //      System.out.println("compare " + r.getMethod().getDeclaringClass());
View Full Code Here

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

            for (Iterator boxes = unit.getUseBoxes().iterator(); boxes
                    .hasNext();) {
                ValueBox box = (ValueBox) boxes.next();
                Value value = box.getValue();

                if (value instanceof CastExpr) {
                    // If the cast is to the same type as the
                    // operand already is, then replace with
                    // simple assignment.
View Full Code Here

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

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

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

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

                    // Inline calls to connections changed.
                    if (r.getMethod().equals(
                            PtolemyUtilities.connectionsChangedMethod)) {
                        // If we are calling connections changed on one of the classes
                        // we are generating code for, then inline it.
                        if (type.getSootClass().isApplicationClass()) {
                            SootMethod inlinee = null;

                            if (r instanceof VirtualInvokeExpr) {
                                // Now inline the resulting call.
                                List methodList = Scene
                                        .v()
                                        .getActiveHierarchy()
                                        .resolveAbstractDispatch(
                                                type.getSootClass(),
                                                PtolemyUtilities.connectionsChangedMethod);

                                if (methodList.size() == 1) {
                                    // Inline the method.
                                    inlinee = (SootMethod) methodList.get(0);
                                } else {
                                    String string = "Can't inline " + stmt
                                            + " in method " + method + "\n";

                                    for (int i = 0; i < methodList.size(); i++) {
                                        string += ("target = "
                                                + methodList.get(i) + "\n");
                                    }

                                    System.out.println(string);
                                }
                            } else if (r instanceof SpecialInvokeExpr) {
                                inlinee = Scene.v().getActiveHierarchy()
                                        .resolveSpecialDispatch(
                                                (SpecialInvokeExpr) r, method);
                            }

                            if (inlinee != null
                                    && !inlinee.getDeclaringClass()
                                            .isApplicationClass()) {
                                inlinee.getDeclaringClass().setLibraryClass();
                            }

                            inlinee.retrieveActiveBody();

                            if (debug) {
                                System.out
                                        .println("Inlining method call: " + r);
                            }

                            SiteInliner.inlineSite(inlinee, stmt, method);

                            doneSomething = true;
                        } else {
                            // FIXME: this is a bit of a hack, but
                            // for right now it seems to work.
                            // How many things that aren't
                            // the actors we are generating
                            // code for do we really care about here?
                            // Can we do this without having to create
                            // a class for the port too????
                            body.getUnits().remove(stmt);
                            doneSomething = true;
                        }
                    }

                    // Statically evaluate constant arguments.
                    Value[] argValues = new Value[r.getArgCount()];
                    int constantArgCount = 0;

                    for (Iterator args = r.getArgs().iterator(); args.hasNext();) {
                        Value arg = (Value) args.next();

                        //System.out.println("arg = " + arg);
                        if (Evaluator.isValueConstantValued(arg)) {
                            argValues[constantArgCount++] = Evaluator
                                    .getConstantValueOf(arg);

                            // System.out.println("argument = " + argValues[argCount-1]);
                        } else {
                            break;
                        }
                    }

                    //boolean allArgsAreConstant = (r.getArgCount() == constantArgCount);

                    if (SootUtilities.derivesFrom(type.getSootClass(),
                            PtolemyUtilities.componentPortClass)) {
                        // If we are invoking a method on a port
                        // class, then attempt to get the constant
                        // value of the port.
                        TypedIOPort port = (TypedIOPort) analysis
                                .getObject((Local) r.getBase());

                        //     System.out.println("reference to port = " + port);
                        if (port == null) {
                            continue;
                        }

                        // If we do this, then we have to get rid of
                        // the ports.
                        if (port instanceof Typeable) {
                            PtolemyUtilities.inlineTypeableMethods(body, stmt,
                                    box, r, port);
                        }

                        // Inline namedObj methods on the attribute.
                        if (r.getMethod().getSubSignature().equals(
                                PtolemyUtilities.getFullNameMethod
                                        .getSubSignature())) {
                            box.setValue(StringConstant.v(port.getFullName()));
                        }

                        if (r.getMethod().getSubSignature().equals(
                                PtolemyUtilities.getNameMethod
                                        .getSubSignature())) {
                            box.setValue(StringConstant.v(port.getName()));
                        }

                        String methodName = r.getMethod().getName();

                        if ((port.getWidth() == 0)
                                && (methodName.equals("hasToken")
                                        || methodName.equals("hasRoom")
                                        || methodName.equals("get") || methodName
                                        .equals("put"))) {
                            // NOTE: broadcast is legal on a zero
                            // width port.
                            // If we try to get on a port with
                            // zero width, then throw a runtime
                            // exception.
                            Local local = SootUtilities
                                    .createRuntimeException(
                                            body,
                                            stmt,
                                            methodName
                                                    + "() called on a port with zero width: "
                                                    + port.getFullName() + "!");
                            body.getUnits().insertBefore(
                                    Jimple.v().newThrowStmt(local), stmt);

                            if (stmt instanceof DefinitionStmt) {
                                // be sure we replace with the
                                // right return type.
                                if (methodName.equals("hasToken")
                                        || methodName.equals("hasRoom")) {
                                    box.setValue(IntConstant.v(0));
                                } else {
                                    box.setValue(NullConstant.v());
                                }
                            } else {
                                body.getUnits().remove(stmt);
                            }

                            continue;
                        }

                        if (r.getMethod().getName().equals("isInput")) {
                            if (debug) {
                                System.out.println("replacing isInput at "
                                        + stmt);
                            }

                            if (port.isInput()) {
                                box.setValue(IntConstant.v(1));
                            } else {
                                box.setValue(IntConstant.v(0));
                            }
                        } else if (r.getMethod().getName().equals("isOutput")) {
                            if (debug) {
                                System.out.println("replacing isOutput at "
                                        + stmt);
                            }

                            if (port.isOutput()) {
                                box.setValue(IntConstant.v(1));
                            } else {
                                box.setValue(IntConstant.v(0));
                            }
                        } else if (r.getMethod().getName()
                                .equals("isMultiport")) {
                            if (debug) {
                                System.out.println("replacing isMultiport at "
                                        + stmt);
                            }

                            if (port.isMultiport()) {
                                box.setValue(IntConstant.v(1));
                            } else {
                                box.setValue(IntConstant.v(0));
                            }
                        } else if (r.getMethod().getName().equals("getWidth")
                                || r.getMethod().getName().equals(
                                        "numberOfSources")
                                || r.getMethod().getName().equals(
                                        "numberOfSinks")) {
                            if (debug) {
                                System.out.println("replacing getWidth at "
                                        + stmt);
                            }

                            // Reflect and invoke the same method on our port
                            Object object = SootUtilities
                                    .reflectAndInvokeMethod(port,
                                            r.getMethod(), argValues);

                            // System.out.println("method result  = " + constant);
                            Constant constant = SootUtilities
                                    .convertArgumentToConstantValue(object);

                            // replace the method invocation.
                            box.setValue(constant);
                        } else if (r.getMethod().getName().equals("hasToken")) {
                            // return true.
                            if (debug) {
                                System.out.println("replacing hasToken at "
                                        + stmt);
                            }

                            box.setValue(IntConstant.v(1));
                        } else if (r.getMethod().getName().equals("hasRoom")) {
                            // return true.
                            if (debug) {
                                System.out.println("replacing hasRoom at "
                                        + stmt);
                            }

                            box.setValue(IntConstant.v(1));
                        } else if (r.getMethod().getName().equals("get")) {
                            // Could be get that takes a channel and
                            // returns a token, or get that takes a
                            // channel and a count and returns an
                            // array of tokens.  In either case,
View Full Code Here

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

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

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

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

                    // Inline calls to connections changed.
                    if (r.getMethod().equals(
                            PtolemyUtilities.connectionsChangedMethod)) {
                        // If we are calling connections changed on one of the classes
                        // we are generating code for, then inline it.
                        if (type.getSootClass().isApplicationClass()) {
                            SootMethod inlinee = null;

                            if (r instanceof VirtualInvokeExpr) {
                                // Now inline the resulting call.
                                List methodList = Scene
                                        .v()
                                        .getActiveHierarchy()
                                        .resolveAbstractDispatch(
                                                type.getSootClass(),
                                                PtolemyUtilities.connectionsChangedMethod);

                                if (methodList.size() == 1) {
                                    // Inline the method.
                                    inlinee = (SootMethod) methodList.get(0);
                                } else {
                                    String string = "Can't inline " + stmt
                                            + " in method " + method + "\n";

                                    for (int i = 0; i < methodList.size(); i++) {
                                        string += ("target = "
                                                + methodList.get(i) + "\n");
                                    }

                                    System.out.println(string);
                                }
                            } else if (r instanceof SpecialInvokeExpr) {
                                inlinee = Scene.v().getActiveHierarchy()
                                        .resolveSpecialDispatch(
                                                (SpecialInvokeExpr) r, method);
                            }

                            if (inlinee != null
                                    && !inlinee.getDeclaringClass()
                                            .isApplicationClass()) {
                                inlinee.getDeclaringClass().setLibraryClass();
                            }

                            inlinee.retrieveActiveBody();

                            if (debug) {
                                System.out
                                        .println("Inlining method call: " + r);
                            }

                            SiteInliner.inlineSite(inlinee, stmt, method);

                            doneSomething = true;
                        } else {
                            // FIXME: this is a bit of a hack, but
                            // for right now it seems to work.
                            // How many things that aren't
                            // the actors we are generating
                            // code for do we really care about here?
                            // Can we do this without having to create
                            // a class for the port too????
                            body.getUnits().remove(stmt);
                            doneSomething = true;
                        }
                    }

                    // Statically evaluate constant arguments.
                    Value[] argValues = new Value[r.getArgCount()];
                    int constantArgCount = 0;

                    for (Iterator args = r.getArgs().iterator(); args.hasNext();) {
                        Value arg = (Value) args.next();

                        //System.out.println("arg = " + arg);
                        if (Evaluator.isValueConstantValued(arg)) {
                            argValues[constantArgCount++] = Evaluator
                                    .getConstantValueOf(arg);

                            // System.out.println("argument = " + argValues[argCount-1]);
                        } else {
                            break;
                        }
                    }

                    //boolean allArgsAreConstant = (r.getArgCount() == constantArgCount);

                    if (SootUtilities.derivesFrom(type.getSootClass(),
                            PtolemyUtilities.componentPortClass)) {
                        // If we are invoking a method on a port
                        // class, then attempt to get the constant
                        // value of the port.
                        TypedIOPort port = (TypedIOPort) analysis
                                .getObject((Local) r.getBase());

                        //     System.out.println("reference to port = " + port);
                        if (port == null) {
                            continue;
                        }

                        /** Don't do this for inside connections, to
                         * allow for properly defined ports of
                         * toplevel composites.
                         */

                        //    if (port instanceof Typeable) {
                        //                             PtolemyUtilities.inlineTypeableMethods(body,
                        //                                     stmt, box, r, (Typeable)port);
                        //                         }
                        // Inline namedObj methods on the attribute.
                        if (r.getMethod().getSubSignature().equals(
                                PtolemyUtilities.getFullNameMethod
                                        .getSubSignature())) {
                            box.setValue(StringConstant.v(port.getFullName()));
                        }

                        if (r.getMethod().getSubSignature().equals(
                                PtolemyUtilities.getNameMethod
                                        .getSubSignature())) {
                            box.setValue(StringConstant.v(port.getName()));
                        }

                        //String methodName = r.getMethod().getName();

                        //   if (port.getWidth() == 0 &&
                        //                                     (methodName.equals("hasToken") ||
                        //                                             methodName.equals("hasRoom") ||
                        //                                             methodName.equals("get") ||
                        //                                             methodName.equals("put"))) {
                        //                                 // NOTE: broadcast is legal on a zero
                        //                                 // width port.
                        //                                 // If we try to get on a port with
                        //                                 // zero width, then throw a runtime
                        //                                 // exception.
                        //                                 Local local = SootUtilities.createRuntimeException(body, stmt,
                        //                                         methodName + "() called on a port with zero width: " +
                        //                                         port.getFullName() + "!");
                        //                                 body.getUnits().insertBefore(Jimple.v().newThrowStmt(local),
                        //                                         stmt);
                        //                                 if (stmt instanceof DefinitionStmt) {
                        //                                     // be sure we replace with the
                        //                                     // right return type.
                        //                                     if (methodName.equals("hasToken") ||
                        //                                             methodName.equals("hasRoom")) {
                        //                                         box.setValue(IntConstant.v(0));
                        //                                     } else {
                        //                                         box.setValue(NullConstant.v());
                        //                                     }
                        //                                 } else {
                        //                                     body.getUnits().remove(stmt);
                        //                                 }
                        //                                 continue;
                        //                             }
                        if (r.getMethod().getName().equals("isInput")) {
                            // return true.
                            if (port.isInput()) {
                                box.setValue(IntConstant.v(1));
                            } else {
                                box.setValue(IntConstant.v(0));
                            }
                        } else if (r.getMethod().getName().equals("isOutput")) {
                            // return true.
                            if (port.isOutput()) {
                                box.setValue(IntConstant.v(1));
                            } else {
                                box.setValue(IntConstant.v(0));
                            }
                        } else if (r.getMethod().getName()
                                .equals("isMultiport")) {
                            // return true.
                            if (port.isMultiport()) {
                                box.setValue(IntConstant.v(1));
                            } else {
                                box.setValue(IntConstant.v(0));
                            }
                        } else if (r.getMethod().getName().equals("getWidth")) {
                            // Reflect and invoke the same method on our port
                            Object object = SootUtilities
                                    .reflectAndInvokeMethod(port,
                                            r.getMethod(), argValues);

                            // System.out.println("method result  = " + constant);
                            Constant constant = SootUtilities
                                    .convertArgumentToConstantValue(object);

                            // replace the method invocation.
                            box.setValue(constant);
                        } else if (r.getMethod().getName().equals("hasToken")) {
                            // return true.
                            if (debug) {
                                System.out.println("inlining hasToken at "
                                        + stmt);
                            }

                            box.setValue(IntConstant.v(1));
                        } else if (r.getMethod().getName().equals("hasRoom")) {
                            // return true.
                            if (debug) {
                                System.out.println("inlining hasRoom at "
                                        + stmt);
                            }

                            box.setValue(IntConstant.v(1));
                        } else if (r.getMethod().getName().equals("getInside")) {
                            // Could be get that takes a channel and
                            // returns a token, or get that takes a
                            // channel and a count and returns an
                            // array of tokens.  In either case,
View Full Code Here

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

                                        if (parameterRef.getIndex() == 0) {
                                            ValueBox box = identityStmt
                                                    .getRightOpBox();
                                            box
                                                    .setValue(Jimple
                                                            .v()
                                                            .newParameterRef(
                                                                    method
                                                                            .getParameterType(0),
                                                                    0));
                                        }
                                    }
                                }
                            }
                        }
                    }

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

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

                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) {
                            // System.out.println("invoke = " + unit);

                            // If we're constructing one of our actor classes,
View Full Code Here

            Stmt unit = (Stmt) units.next();

            // If any box is removable, then remove the statement.
            for (Iterator boxes = unit.getUseBoxes().iterator(); boxes
                    .hasNext();) {
                ValueBox box = (ValueBox) boxes.next();

                // FIXME: This is currently way too simple.
                Value value = box.getValue();
                /*Type type = */value.getType();

                if (value instanceof NewExpr) {
                    // Fix kernel exceptions to be runtime exceptions.
                    NewExpr expr = (NewExpr) value;
                    SootClass exceptionClass = expr.getBaseType()
                            .getSootClass();

                    if (_isPtolemyException(exceptionClass)) {
                        expr.setBaseType(RefType
                                .v(PtolemyUtilities.runtimeExceptionClass));
                    }
                } else if (value instanceof CastExpr) {
                    CastExpr expr = (CastExpr) value;
                    Type castType = expr.getCastType();

                    if (castType instanceof RefType
                            && _isPtolemyException(((RefType) castType)
                                    .getSootClass())) {
                        expr.setCastType(RefType
                                .v(PtolemyUtilities.runtimeExceptionClass));
                    }
                } else if (value instanceof SpecialInvokeExpr) {
                    // Fix the exception constructors.
                    SpecialInvokeExpr expr = (SpecialInvokeExpr) value;
                    SootClass exceptionClass = ((RefType) expr.getBase()
                            .getType()).getSootClass();

                    if (_isPtolemyException(exceptionClass)) {
                        Value foundArg = null;

                        for (Iterator args = expr.getArgs().iterator(); args
                                .hasNext();) {
                            Value arg = (Value) args.next();

                            if (arg.getType().equals(
                                    RefType.v(PtolemyUtilities.stringClass))) {
                                foundArg = arg;
                                break;
                            }
                        }

                        if ((foundArg == null) || _obfuscate) {
                            box
                                    .setValue(Jimple
                                            .v()
                                            .newSpecialInvokeExpr(
                                                    (Local) expr.getBase(),
                                                    PtolemyUtilities.runtimeExceptionConstructor
                                                            .makeRef(),
                                                    Collections.EMPTY_LIST));
                        } else {
                            box
                                    .setValue(Jimple
                                            .v()
                                            .newSpecialInvokeExpr(
                                                    (Local) expr.getBase(),
                                                    PtolemyUtilities.runtimeExceptionStringConstructor
                                                            .makeRef(),
                                                    foundArg));
                        }
                    }
                } else if (value instanceof VirtualInvokeExpr) {
                    VirtualInvokeExpr expr = (VirtualInvokeExpr) value;
                    Type exceptionType = expr.getBase().getType();

                    if (exceptionType instanceof RefType) {
                        SootClass exceptionClass = ((RefType) exceptionType)
                                .getSootClass();

                        if (_isPtolemyException(exceptionClass)) {
                            SootMethod method = expr.getMethod();

                            if (method.getName().equals("getMessage")) {
                                if (unit instanceof InvokeStmt) {
                                    body.getUnits().remove(unit);
                                } else {
                                    box.setValue(StringConstant
                                            .v("PtolemyException"));
                                }
                            }
                        }
                    }
View Full Code Here

TOP

Related Classes of soot.ValueBox

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