Package soot.jimple

Examples of soot.jimple.InstanceInvokeExpr


    }
    public void addType( Local receiver, Context srcContext, Type type, Context typeContext ) {
        FastHierarchy fh = Scene.v().getOrMakeFastHierarchy();
        for( Iterator siteIt = ((Collection) receiverToSites.get( receiver )).iterator(); siteIt.hasNext(); ) {
            final VirtualCallSite site = (VirtualCallSite) siteIt.next();
            InstanceInvokeExpr iie = site.iie();
            if( site.kind() == Kind.THREAD
            && !fh.canStoreType( type, clRunnable ) )
                continue;

            if( site.iie() instanceof SpecialInvokeExpr && site.kind != Kind.THREAD ) {
View Full Code Here


            final Stmt s = (Stmt) sIt.next();
            if (s.containsInvokeExpr()) {
                InvokeExpr ie = s.getInvokeExpr();

                if (ie instanceof InstanceInvokeExpr) {
                    InstanceInvokeExpr iie = (InstanceInvokeExpr) ie;
                    Local receiver = (Local) iie.getBase();
                    NumberedString subSig =
                        iie.getMethodRef().getSubSignature();
                    addVirtualCallSite( s, m, receiver, iie, subSig,
                            Edge.ieToKind(iie) );
                    if( subSig == sigStart ) {
                        addVirtualCallSite( s, m, receiver, iie, sigRun,
                                Kind.THREAD );
View Full Code Here

        }

        if (value instanceof VirtualInvokeExpr
                || value instanceof InterfaceInvokeExpr
                || value instanceof SpecialInvokeExpr) {
            InstanceInvokeExpr r = (InstanceInvokeExpr) value;

            Local local = (Local) r.getBase();
            Type baseType = local.getType();

            if (baseType instanceof NullType) {
                // replace with NullPointerException..
                if (debug) {
                    System.out.println("Invoke base is null, replacing with "
                            + "NullPointerException");
                }

                Local exception = SootUtilities.createRuntimeException(body,
                        unit, "NullPointerException");
                body.getUnits().insertBefore(
                        Jimple.v().newThrowStmt(exception), unit);
                body.getUnits().remove(unit);
            }

            boolean isInlineableTokenMethod = _isLocalTokenTypeWithDepth(local,
                    typeAnalysis, unsafeLocalSet, depth, debug);

            if (debug) {
                System.out.println("checking inline for " + r);
            }

            // Check if token arguments are being used.  This makes
            // sure we get methods like Scale._scaleOnRight and
            // DB._doFunction inlined.  It would be better if the
            // token inliner modified the functions, but it doesn't.
            if (baseType instanceof RefType
                    && Scene.v().getApplicationClasses().contains(
                            ((RefType) baseType).getSootClass())) {
                Type returnType = r.getMethod().getReturnType();
                isInlineableTokenMethod |= _isInlineableTokenType(returnType);

                for (Iterator args = r.getArgs().iterator(); args.hasNext()
                        && !isInlineableTokenMethod;) {
                    Object arg = args.next();

                    if (arg instanceof Local) {
                        Local argLocal = (Local) arg;

                        if (debug) {
                            System.out.println("argtype = "
                                    + argLocal.getType());
                        }

                        isInlineableTokenMethod = _isLocalTokenTypeWithDepth(
                                argLocal, typeAnalysis, unsafeLocalSet, depth,
                                debug);

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

            if (!isInlineableTokenMethod) {
                return false;
            }

            // System.out.println("baseType = " + baseType);
            RefType type = (RefType) typeAnalysis.getSpecializedSootType(local);

            // System.out.println("specializedType = " + type);
            // Then determine the method that was
            // actually invoked.
            List methodList;

            if (value instanceof SpecialInvokeExpr) {
                SootMethod targetMethod = hierarchy.resolveSpecialDispatch(
                        (SpecialInvokeExpr) r, method);
                methodList = new LinkedList();
                methodList.add(targetMethod);
            } else {
                methodList =
                //      invokeGraph.getTargetsOf((Stmt)unit);
                hierarchy.resolveAbstractDispatch(type.getSootClass(), r
                        .getMethod());
            }

            // If there was only one possible method...
            if (methodList.size() == 1) {
                // Then inline its code
                SootMethod inlinee = (SootMethod) methodList.get(0);

                if (inlinee.getName().equals("getClass")) {
                    SootClass typeClass = type.getSootClass();
                    int subclasses = hierarchy.getSubclassesOf(typeClass)
                            .size();

                    if (subclasses == 0) {
                        // FIXME: do something better here.
                        SootMethod newGetClassMethod = Scene.v().getMethod(
                                "<java.lang.Class: java.lang.Class "
                                        + "forName(java.lang.String)>");
                        box.setValue(Jimple.v().newStaticInvokeExpr(
                                newGetClassMethod.makeRef(),
                                StringConstant.v(typeClass.getName())));
                        doneSomething = true;
                    }
                } else if (inlinee.getName().equals("isNil")) {
                    box.setValue(IntConstant.v(0));
                    //                 } else if (inlinee.getSignature().equals(PtolemyUtilities.arrayTokenConstructor.getSignature())) {
                    //                     System.out.println("method = " + method);
                    //                     System.out.println("inlinee = " + inlinee.getSignature());
                    //                     throw new RuntimeException("Cannot inline arrayTokens that do not have a type explicitly specified.");
                } else {
                    SootClass declaringClass = inlinee.getDeclaringClass();

                    if (!declaringClass.isApplicationClass()) {
                        declaringClass.setLibraryClass();
                    }

                    if (!inlinee.isAbstract() && !inlinee.isNative()) {
                        // FIXME: only inline things where we are
                        // also inlining the constructor???
                        if (debug) {
                            System.out.println("inlining " + inlinee);
                        }

                        inlinee.retrieveActiveBody();

                        // Then we know exactly what method will
                        // be called, so inline it.
                        SiteInliner.inlineSite(inlinee, (Stmt) unit, method);
                        doneSomething = true;
                    } else {
                        throw new RuntimeException("inlinee is not concrete!: "
                                + inlinee);
                    }
                }
            } else {
                if (debug) {
                    System.out.println("uninlinable method invocation = " + r);

                    for (Iterator j = methodList.iterator(); j.hasNext();) {
                        System.out.println("method = " + j.next());
                    }
                }
            }
        } else if (value instanceof SpecialInvokeExpr) {
            SpecialInvokeExpr r = (SpecialInvokeExpr) value;

            if (debug) {
                System.out.println("special invoking = " + r.getMethod());
            }

            Type baseType = typeAnalysis.getSpecializedSootType((Local) r
                    .getBase());

            if (baseType instanceof RefType) {
                RefType type = (RefType) baseType;

                boolean isInlineableTokenMethod = SootUtilities.derivesFrom(
                        type.getSootClass(), PtolemyUtilities.tokenClass);

                // If it is a token, then check to
                // make sure that it has the
                // appropriate type
                if (isInlineableTokenMethod) {
                    type = (RefType) typeAnalysis
                            .getSpecializedSootType((Local) r.getBase());

                    if (PtolemyUtilities.getTypeDepth(typeAnalysis
                            .getSpecializedType((Local) r.getBase())) != depth) {
                        if (debug) {
                            System.out
                                    .println("skipping, type depth = "
                                            + PtolemyUtilities
                                                    .getTypeDepth(typeAnalysis
                                                            .getSpecializedType((Local) r
                                                                    .getBase()))
                                            + ", but only inlining depth "
                                            + depth);
                        }

                        return false;

                        //continue;
                    }
                }

                if (isInlineableTokenMethod) {
                    SootMethod inlinee = hierarchy.resolveSpecialDispatch(r,
                            method);
                    SootClass declaringClass = inlinee.getDeclaringClass();

                    if (!declaringClass.isApplicationClass()) {
                        declaringClass.setLibraryClass();
                    }

                    if (!inlinee.isAbstract() && !inlinee.isNative()) {
                        if (debug) {
                            System.out.println("inlining");
                        }

                        inlinee.retrieveActiveBody();

                        // Then we know exactly what method will
                        // be called, so inline it.
                        SiteInliner.inlineSite(inlinee, (Stmt) unit, method);
                        doneSomething = true;
                    } else {
                        if (debug) {
                            System.out.println("removing");
                        }

                        // If we don't have a method,
                        // then remove the invocation.
                        body.getUnits().remove(unit);
                    }
                }
            }
        } else if (value instanceof StaticInvokeExpr) {
            StaticInvokeExpr r = (StaticInvokeExpr) value;

            // Inline typelattice methods.
            if (r.getMethod().getDeclaringClass().equals(
                    PtolemyUtilities.typeLatticeClass)) {
                try {
                    if (debug) {
                        System.out.println("inlining typelattice method = "
                                + unit);
                    }

                    typeAnalysis.inlineTypeLatticeMethods(method, unit, box, r,
                            localDefs, localUses);
                } catch (Exception ex) {
                    ex.printStackTrace();
                    System.out.println("Exception occurred " + ex.getMessage());
                }
            } else {
                if (debug) {
                    System.out.println("static invoking = " + r.getMethod());
                }

                SootMethod inlinee = r.getMethod();
                SootClass declaringClass = inlinee.getDeclaringClass();
                Type returnType = inlinee.getReturnType();

                // These methods contain a large amount of
                // code, which greatly slows down further
View Full Code Here

        Hierarchy hierarchy = Scene.v().getActiveHierarchy();
        Value value = box.getValue();

        if (value instanceof VirtualInvokeExpr
                || value instanceof InterfaceInvokeExpr) {
            InstanceInvokeExpr r = (InstanceInvokeExpr) value;

            Type baseType = r.getBase().getType();

            // If we are invoking a method on a Token or Type, and
            // the token is not unsafe.
            if (baseType instanceof RefType
                    && !unsafeLocalSet.contains(r.getBase())) {
                RefType type = (RefType) baseType;

                boolean isInlineableTypeMethod = SootUtilities.derivesFrom(type
                        .getSootClass(), PtolemyUtilities.typeClass)
                        && (r.getMethod().getName().equals("convert")
                                || r.getMethod().getName().equals("equals")
                                || r.getMethod().getName().equals(
                                        "getTokenClass") || r.getMethod()
                                .getName().equals("getElementType"));

                if (debug && isInlineableTypeMethod) {
                    System.out.println("Inlineable type method = "
                            + r.getMethod());
                }

                if (isInlineableTypeMethod) {
                    ptolemy.data.type.Type baseTokenType = typeAnalysis
                            .getSpecializedType((Local) r.getBase());
                    SootClass typeClass = Scene.v().loadClassAndSupport(
                            baseTokenType.getClass().getName());

                    // Only instantiable types can be inlined..
                    if (!baseTokenType.isInstantiable()) {
                        return doneSomething;
                    }

                    // Then determine the method that was
                    // actually invoked.
                    List methodList = hierarchy.resolveAbstractDispatch(
                            typeClass, r.getMethod());

                    // If there was only one possible method...
                    if (methodList.size() == 1) {
                        // Then inline its code
                        SootMethod inlinee = (SootMethod) methodList.get(0);

                        if (inlinee.getName().equals("getClass")) {
                            // FIXME: do something better here.
                            SootMethod newGetClassMethod = Scene.v().getMethod(
                                    "<java.lang.Class: java.lang.Class "
                                            + "forName(java.lang.String)>");
                            box.setValue(Jimple.v().newStaticInvokeExpr(
                                    newGetClassMethod.makeRef(),
                                    StringConstant.v("java.lang.Object")));
                        } else if (inlinee.getName().equals("getElementType")) {
                            if (debug) {
                                System.out.println("handling getElementType: "
                                        + unit);
                            }

                            // Handle ArrayType.getElementType specially.
                            if (baseTokenType instanceof ptolemy.data.type.ArrayType) {
                                Local local = PtolemyUtilities
                                        .buildConstantTypeLocal(
                                                body,
                                                unit,
                                                ((ptolemy.data.type.ArrayType) baseTokenType)
                                                        .getElementType());
                                box.setValue(local);
                                doneSomething = true;
                            }
                        } else if (inlinee.getName().equals("equals")) {
                            try {
                                if (debug) {
                                    System.out.println("handling equals: "
                                            + unit);
                                }

                                // Handle Type.equals
                                ptolemy.data.type.Type argumentTokenType =
                                //                                     PtolemyUtilities
                                //                                         .getTypeValue(method, (Local) r
                                //                                                 .getArg(0), unit, localDefs,
                                //                                                 localUses);

                                typeAnalysis.getSpecializedType((Local) r
                                        .getArg(0));
                                System.out.println("baseTokenType = "
                                        + baseTokenType);
                                System.out.println("argumentTokenType = "
                                        + baseTokenType);
View Full Code Here

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

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

                        if (_debug) {
                            System.out.println("invoking = " + r.getMethod());
                        }

                        // Skip initializers.
                        if (r.getMethod().getName().equals("<init>")) {
                            continue;
                        }

                        doneSomething |= _replaceTokenInvocation(body, stmt,
                                box, r);
View Full Code Here

                        //                             box.setValue(Jimple.v().newParameterRef(
                        //                                     RefType.v(newClass), r.getIndex()));
                        //                         }
                    } else if (value instanceof InstanceInvokeExpr) {
                        // Fix up the method invokes.
                        InstanceInvokeExpr r = (InstanceInvokeExpr) value;

                        try {
                            if (object != analysis.getObject((Local) r
                                    .getBase())) {
                                //                                 System.out.println("object = " + object);
                                //                                 System.out.println("analysis object = " + analysis.getObject((Local)r.getBase()));
                                //                                 System.out.println("not equal!");
                                continue;
                            }
                        } catch (Exception ex) {
                            if (_debug) {
                                System.out.println("Exception on invoke = "
                                        + ex);
                            }

                            continue;
                        }

                        if (SootUtilities.derivesFrom(oldClass, r.getMethod()
                                .getDeclaringClass())) {
                            if (newClass.declaresMethod(r.getMethod()
                                    .getSubSignature())) {
                                SootMethod replacementMethod = newClass
                                        .getMethod(r.getMethod()
                                                .getSubSignature());
                                r.setMethodRef(replacementMethod.makeRef());
                            }

                            //                         } else if (r.getMethod().getDeclaringClass().getName().
                            //                                 startsWith(oldClass.getName())) {
                            //                             SootClass changeClass =
                            //                                 _getInnerClassCopy(oldClass,
                            //                                         r.getMethod().getDeclaringClass(),
                            //                                         newClass);
                            //                             r.setMethod(changeClass.getMethod(
                            //                                     r.getMethod().getSubSignature()));
                        }
                    } else if (value instanceof NewExpr) {
                        // Fix up the object creations.
                        NewExpr r = (NewExpr) value;

                        if (!(unit instanceof AssignStmt)) {
                            continue;
                        }

                        AssignStmt stmt = (AssignStmt) unit;

                        try {
                            if (object != analysis.getObject((Local) stmt
                                    .getLeftOp())) {
                                continue;
                            }
                        } catch (Exception ex) {
                            if (_debug) {
                                System.out.println("Exception on new = " + ex);
                            }

                            continue;
                        }

                        if (r.getBaseType().getSootClass() == oldClass) {
                            r.setBaseType(RefType.v(newClass));

                            //   System.out.println("newValue = " +
                            //           box.getValue());
                            //                         } else if (r.getBaseType().getSootClass().getName().
                            //                                 startsWith(oldClass.getName())) {
View Full Code Here

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

                if (box.getValue() instanceof InstanceInvokeExpr) {
                    InstanceInvokeExpr expr = (InstanceInvokeExpr) box
                            .getValue();
                    Local local = (Local) expr.getBase();

                    if (local == thisLocal) {
                        System.out.println("fixing invoke = " + expr);
                        box.setValue(Jimple.v().newStaticInvokeExpr(
                                expr.getMethod().makeRef(), expr.getArgs()));
                    }
                } else if (box.getValue() instanceof InstanceFieldRef) {
                    InstanceFieldRef expr = (InstanceFieldRef) box.getValue();
                    Local local = (Local) expr.getBase();

                    if (local == thisLocal) {
                        System.out.println("fixing field = " + expr);
                        box.setValue(Jimple.v().newStaticFieldRef(
                                expr.getField().makeRef()));
                    }
                }
            }

            // Fix synchronization locks.  Anything synchronized on this
View Full Code Here

                    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

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

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

                if (debug) {
                    System.out.println("invoking = " + r.getMethod());
                }

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

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

                    // Remove calls to validate().
                    if (r.getMethod().equals(PtolemyUtilities.validateMethod)) {
                        body.getUnits().remove(stmt);
                        continue;
                    } else {
                        // Inline calls to attribute changed that do
                        // not occur from within another
                        // attributeChanged method.
                        if (r.getMethod().getSubSignature().equals(
                                PtolemyUtilities.attributeChangedMethod
                                        .getSubSignature())) {
                            // If we are calling attribute 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) {
                                    inlinee = SootUtilities
                                            .resolveVirtualInvokationForInlining(
                                                    type.getSootClass(),
                                                    PtolemyUtilities.attributeChangedMethod);
                                } else if (r instanceof SpecialInvokeExpr) {
                                    inlinee = SootUtilities
                                            .resolveSpecialInvokationForInlining(
                                                    (SpecialInvokeExpr) r,
                                                    method);
                                }

                                if (inlinee.equals(method)) {
                                    System.out
                                            .println("Skipping inline at "
                                                    + r
                                                    + " because we can't inline methods into themselves.");
                                } else {
                                    if (debug) {
                                        System.out
                                                .println("Inlining method call: "
                                                        + r);
                                    }

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

                                    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 attribute too????
                                body.getUnits().remove(stmt);
                                doneSomething = true;
                            }
                        }
                    }

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

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

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

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

                    if (SootUtilities.derivesFrom(type.getSootClass(),
                            PtolemyUtilities.settableClass)) {
                        // If we are invoking a method on a
                        // variable class, then attempt to get the
                        // constant value of the variable.
                        Attribute attribute = (Attribute) namedObjAnalysis
                                .getObject((Local) r.getBase());

                        if (debug) {
                            System.out.println("Settable base = " + attribute);
                        }

                        // If the attribute resolves to null, then
                        // replace the invocation with an exception
                        // throw.  It would be nice to do this, but
                        // some strange cases fail as a result, in
                        // particular, if you showName on a
                        // PortParameter it fails (!)
                        //                         if (attribute == null) {
                        //                             if (debug) System.out.println("replacing with NullPointerException!");
                        //                             Local exceptionLocal =
                        //                                 SootUtilities.createRuntimeException(
                        //                                         body, stmt,
                        //                                         "NullPointerException: " + r);
                        //                             body.getUnits().swapWith(stmt,
                        //                                     Jimple.v().newThrowStmt(
                        //                                             exceptionLocal));
                        //                             continue;
                        //                         }
                        // Inline getType, setTypeEquals, etc...
                        if (attribute instanceof Typeable) {
                            if (PtolemyUtilities.inlineTypeableMethods(body,
                                    stmt, box, r, (Typeable) attribute)) {
                                continue;
                            }
                        }

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

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

                        if (r instanceof SpecialInvokeExpr) {
                            continue;
                        }

                        // Get some references to the container of the
                        // attribute.
                        if (attribute == null) {
                            throw new InternalErrorException(
                                    "Attribute == null?, "
                                            + "this should not be happening! "
                                            + "\n\tInstanceInvokeExpr: "
                                            + r
                                            + "\n\tbase: "
                                            + r.getBase()
                                            + "\n\tmethod: "
                                            + method
                                            + "\n\treferredObject: "
                                            + referredObject
                                            + "\nGetting 'null' out of the "
                                            + "TypeAnalysis means "
                                            + "'I can't figure "
                                            + "out where this came "
                                            + "from and hence, I can't tell "
                                            + "you what the type is' "
                                            + "The type might be incomparable, "
                                            + "or maybe not! The type analysis "
                                            + "is rather hairy to debug because "
                                            + "it happens multiple times and "
                                            + "is self dependent...  A bug in "
                                            + "one place tends to propagate "
                                            + "to a method inline, which "
                                            + "propagates to another type "
                                            + "analysis . . .");
                        }

                        Entity container = FieldsForEntitiesTransformer
                                .getEntityContainerOfObject(attribute);
                        /*Local thisLocal =*/body.getThisLocal();

                        Local containerLocal = getAttributeContainerRef(
                                container, method, (Local) r.getBase(), stmt,
                                localDefs, localUses, stmt);

                        // FieldsForEntitiesTransformer.getLocalReferenceForEntity(
                        //                                 container, theClass, thisLocal, body, stmt, _options);
                        // For Variables, we handle get/setToken,
                        // get/setExpression different from other
                        // settables
                        if (attribute instanceof Variable) {
                            // Deal with tricky methods separately.
                            // Match the subsignature so we catch
                            // isomorphic subclasses as well...
                            if (r
                                    .getMethod()
                                    .getSubSignature()
                                    .equals(
                                            PtolemyUtilities.variableConstructorWithToken
                                                    .getSubSignature())) {
                                SootClass variableClass = r.getMethod()
                                        .getDeclaringClass();
                                SootMethod constructorWithoutToken = variableClass
                                        .getMethod(PtolemyUtilities.variableConstructorWithoutToken
                                                .getSubSignature());

                                // Replace the three-argument
                                // constructor with a two-argument
                                // constructor.  We do this for
                                // several reasons:
                                // 1) The assignment is
                                // redundant...  all parameters
                                // are initialized with the
                                // appropriate value.
                                // 2) The type of the token is
                                // often wrong for polymorphic
                                // actors.
                                // 3) Later on, when we inline all
                                // token constructors, there is no
                                // longer a token to pass to the
                                // constructor.  It is easier to
                                // just deal with it now...
                                // Create a new two-argument constructor.
                                box.setValue(Jimple.v().newSpecialInvokeExpr(
                                        (Local) r.getBase(),
                                        constructorWithoutToken.makeRef(),
                                        r.getArg(0), r.getArg(1)));

                                // Call setToken with the actual value of the parameter
                                Token token;

                                // First create a token with the given
                                // expression and then set the
                                // token to that value.
                                try {
                                    token = ((Variable) attribute).getToken();
                                } catch (Exception ex) {
                                    throw new RuntimeException(
                                            "Illegal parameter value = "
                                                    + argValues[0]);
                                }

                                String localName = "_CGTokenLocal";
                                Local tokenLocal = PtolemyUtilities
                                        .buildConstantTokenLocal(body, stmt,
                                                token, localName);

                                body
                                        .getUnits()
                                        .insertAfter(
                                                Jimple
                                                        .v()
                                                        .newInvokeStmt(
                                                                Jimple
                                                                        .v()
                                                                        .newVirtualInvokeExpr(
                                                                                (Local) r
                                                                                        .getBase(),
                                                                                PtolemyUtilities.variableSetTokenMethod
                                                                                        .makeRef(),
                                                                                tokenLocal)),
                                                stmt);
                                doneSomething = true;
                            } else if (r.getMethod().getName().equals(
                                    "getToken")) {
                                if (debug) {
                                    System.out
                                            .println("Replacing getToken on Variable");
                                }

                                // replace the method call with a field ref.
                                SootField tokenField = (SootField) attributeToValueFieldMap
                                        .get(attribute);

                                if (tokenField == null) {
                                    throw new RuntimeException(
                                            "No tokenField found for attribute "
                                                    + attribute);
                                }

                                if (stmt instanceof InvokeStmt) {
                                    body.getUnits().remove(stmt);
                                } else {
                                    box.setValue(Jimple.v()
                                            .newInstanceFieldRef(
                                                    containerLocal,
                                                    tokenField.makeRef()));
                                }

                                doneSomething = true;
                            } else if (r.getMethod().getName().equals(
                                    "setToken")
                                    || r.getMethod().getName().equals(
                                            "_setTokenAndNotify")) {
                                if (debug) {
                                    System.out
                                            .println("Replacing setToken on Variable");
                                }

                                // replace the entire statement
                                // (which must be an invokeStmt anyway)
                                // with an assignment to the field of the first argument.
                                SootField tokenField = (SootField) attributeToValueFieldMap
                                        .get(attribute);

                                if (tokenField == null) {
                                    throw new RuntimeException(
                                            "No tokenField found for attribute "
                                                    + attribute);
                                }

                                Local tokenLocal = Jimple.v().newLocal(
                                        "convertedToken", tokenField.getType());
                                body.getLocals().add(tokenLocal);

                                // Convert the token to the type of the variable.
                                Local typeLocal = PtolemyUtilities
                                        .buildConstantTypeLocal(body, stmt,
                                                ((Variable) attribute)
                                                        .getType());
                                body
                                        .getUnits()
                                        .insertBefore(
                                                Jimple
                                                        .v()
                                                        .newAssignStmt(
                                                                tokenLocal,
                                                                Jimple
                                                                        .v()
                                                                        .newInterfaceInvokeExpr(
                                                                                typeLocal,
                                                                                PtolemyUtilities.typeConvertMethod
                                                                                        .makeRef(),
                                                                                r
                                                                                        .getArg(0))),
                                                stmt);
                                body.getUnits().insertBefore(
                                        Jimple.v().newAssignStmt(
                                                tokenLocal,
                                                Jimple.v().newCastExpr(
                                                        tokenLocal,
                                                        tokenField.getType())),
                                        stmt);

                                // Assign the value field for the variable.
                                body.getUnits().insertBefore(
                                        Jimple.v().newAssignStmt(
                                                Jimple.v().newInstanceFieldRef(
                                                        containerLocal,
                                                        tokenField.makeRef()),
                                                tokenLocal), stmt);

                                // Invoke attributeChanged on the
                                // container of the variable.
                                PtolemyUtilities.callAttributeChanged(
                                        containerLocal, (Local) r.getBase(),
                                        theClass, method, body, stmt);

                                // remove the old call.
                                body.getUnits().remove(stmt);
                                doneSomething = true;
                            } else if (r.getMethod().getSubSignature().equals(
                                    PtolemyUtilities.getExpressionMethod
                                            .getSubSignature())) {
                                if (debug) {
                                    System.out
                                            .println("Replacing getExpression on Variable");
                                }

                                // First get the token out of the
                                // field, and then insert a call to
                                // its toString method to get the
                                // expression.
                                SootField tokenField = (SootField) attributeToValueFieldMap
                                        .get(attribute);

                                if (tokenField == null) {
                                    throw new RuntimeException(
                                            "No tokenField found for attribute "
                                                    + attribute);
                                }

                                String localName = "_CGTokenLocal";
                                Local tokenLocal = Jimple.v().newLocal(
                                        localName, tokenField.getType());
                                body.getLocals().add(tokenLocal);

                                body.getUnits().insertBefore(
                                        Jimple.v().newAssignStmt(
                                                tokenLocal,
                                                Jimple.v().newInstanceFieldRef(
                                                        containerLocal,
                                                        tokenField.makeRef())),
                                        stmt);
                                box.setValue(Jimple.v().newVirtualInvokeExpr(
                                        tokenLocal,
                                        PtolemyUtilities.toStringMethod
                                                .makeRef()));
                                doneSomething = true;

                                // FIXME null result => ""
                            } else if (false) { // (r.getMethod().getSubSignature().equals(PtolemyUtilities.setExpressionMethod.getSubSignature())) {

                                // FIXME: 1/08: This section used to
                                // never be run, but the change to
                                // NonStrictTest.wrapup() requires it.
                                // To test this, run copernicus on
                                // ../../actor/lib/test/auto/Const.xml

                                // Set Expression is problematic
                                // because the expression might not be
                                // known.  We used to assume that this
                                // was part of creation, but now all
                                // the initialization code is removed
                                // early on.  Beware variables used to
                                // evaluate expressions!
                                if (debug) {
                                    System.out
                                            .println("Replacing setExpression on Variable");
                                }

                                // Call attribute changed AFTER we set the token.
                                PtolemyUtilities.callAttributeChanged(
                                        containerLocal, (Local) r.getBase(),
                                        theClass, method, body, body.getUnits()
                                                .getSuccOf(stmt));

                                Token token;

                                // First create a token with the given
                                // expression and then set the
                                // token to that value.
                                try {
                                    token = ((Variable) attribute).getToken();
                                } catch (Exception ex) {
                                    throw new RuntimeException(
                                            "Illegal parameter value = "
                                                    + argValues[0]);
                                }

                                // Create code to instantiate the token
                                SootField tokenField = (SootField) attributeToValueFieldMap
                                        .get(attribute);

                                if (tokenField == null) {
                                    throw new RuntimeException(
                                            "No tokenField found for attribute "
                                                    + attribute);
                                }

                                String localName = "_CGTokenLocal";
                                Local tokenLocal = PtolemyUtilities
                                        .buildConstantTokenLocal(body, stmt,
                                                token, localName);

                                body.getUnits().swapWith(
                                        stmt,
                                        Jimple.v().newAssignStmt(
                                                Jimple.v().newInstanceFieldRef(
                                                        containerLocal,
                                                        tokenField.makeRef()),
                                                tokenLocal));
                                doneSomething = true;
                            } else if (r.getMethod().getName().equals("update")) {
                                // FIXME: for PortParameters.
                                // Now inline the resulting call.
                                SootMethod inlinee = null;

                                if (r instanceof VirtualInvokeExpr) {
                                    inlinee = SootUtilities
                                            .resolveVirtualInvokationForInlining(
                                                    type.getSootClass(),
                                                    PtolemyUtilities.portParameterUpdateMethod);
                                } else if (r instanceof SpecialInvokeExpr) {
                                    inlinee = SootUtilities
                                            .resolveSpecialInvokationForInlining(
                                                    (SpecialInvokeExpr) r,
                                                    method);
                                }

                                SiteInliner.inlineSite(inlinee, stmt, method);
                            } else if (r.getMethod().getName().equals(
                                    "setUnknown")) {
                                // FIXME: for PortParameters.
                                body.getUnits().remove(stmt);
                            } else if (r.getMethod().getSubSignature().equals(
                                    PtolemyUtilities.portParameterGetPortMethod
                                            .getSubSignature())) {
                                //   PortParameter parameter =
                                //                                     (PortParameter)attribute;
                                //                                 ParameterPort port = parameter.getPort();
                                //                                 SootField field =
                                //                                     FieldsForPortsTransformer.getPortField(
                                //                                             port);
                                //                                 box.setValue(Jimple.v().newInstanceFieldRef(
                                //                                                      body.getThisLocal(), field));
                            } else if (r.getMethod().getName().equals(
                                    "addChoice")) {
                                // Ignoring...  does it matter?
                                body.getUnits().remove(stmt);
                            } else if (r.getMethod().getName().equals("_debug")) {
                                // Ignoring...  does it matter?
                                body.getUnits().remove(stmt);
                            } else if (r.getMethod().getName().equals(
                                    "setStringMode")) {
                                // Ignoring...  does it matter?
                                body.getUnits().remove(stmt);
                            } else if (r.getMethod().getName().equals(
                                    "setPersistent")) {
                                // Ignoring...  does it matter?
                                body.getUnits().remove(stmt);
                            } else if (r.getMethod().getName().equals(
                                    "setVisibility")) {
                                // Ignoring...  does it matter?
                                body.getUnits().remove(stmt);
                            } else if (r.getMethod().getName().equals(
                                    "validate")) {
                                // Ignoring...  does it matter?
                                body.getUnits().remove(stmt);
                            } else if (r.getMethod().getName().equals(
                                    "getClass")) {
                                // Don't remove. Token unboxing might
                                // deal with this.
                            } else {
                                if (!r.getMethod().getDeclaringClass()
                                        .isApplicationClass()) {
                                    throw new RuntimeException(
                                            "Found unknown "
                                                    + "variable method invocation of method "
                                                    + r.getMethod()
                                                    + " "
                                                    + referredObject
                                                    + " that cannot be removed!");
                                }
                            }
                        } else { // if (false) { //FIXME

                            // It's just settable, so handle get/setExpression
                            if (r.getMethod().getSubSignature().equals(
                                    PtolemyUtilities.getExpressionMethod
                                            .getSubSignature())) {
                                if (debug) {
                                    System.out
                                            .println("Replacing getExpression on Settable");
                                }

                                box.setValue(Jimple.v().newInstanceFieldRef(
                                        containerLocal,
                                        ((SootField) attributeToValueFieldMap
                                                .get(attribute)).makeRef()));
                                doneSomething = true;
                            } else if (r.getMethod().getSubSignature().equals(
                                    PtolemyUtilities.setExpressionMethod
                                            .getSubSignature())) {
                                if (debug) {
                                    System.out
                                            .println("Replacing setExpression on Settable");
                                }

                                // Call attribute changed AFTER we set the token.
                                PtolemyUtilities.callAttributeChanged(
                                        containerLocal, (Local) r.getBase(),
                                        theClass, method, body, body.getUnits()
                                                .getSuccOf(stmt));

                                // replace the entire statement (which must be an invokeStmt anyway)
                                // with an assignment to the field of the first argument.
                                body
                                        .getUnits()
                                        .swapWith(
                                                stmt,
                                                Jimple
                                                        .v()
                                                        .newAssignStmt(
                                                                Jimple
                                                                        .v()
                                                                        .newInstanceFieldRef(
                                                                                containerLocal,
                                                                                ((SootField) attributeToValueFieldMap
                                                                                        .get(attribute))
                                                                                        .makeRef()),
                                                                r.getArg(0)));
                                doneSomething = true;
                            } else if (r.getMethod().getName().equals(
                                    "setPersistent")) {
                                // Ignoring...  does it matter?
                                body.getUnits().remove(stmt);
                            } else if (r.getMethod().getName().equals(
                                    "setVisibility")) {
                                // Ignoring...  does it matter?
                                body.getUnits().remove(stmt);
                            } else if (r.getMethod().getName().equals(
                                    "validate")) {
                                // Ignoring...  does it matter?
                                body.getUnits().remove(stmt);
                            } else if (r.getMethod().getName().equals(
                                    "getClass")) {
                                // Don't remove. Token unboxing might
                                // deal with this.
                            } else {
                                throw new RuntimeException(
                                        "Found unknown "
                                                + "settable method invocation of method "
                                                + r.getMethod()
                                                + " that cannot be removed!");
                            }
                        }

                        /*
 
View Full Code Here

                if (r.getMethod().equals(PtolemyUtilities.arraycopyMethod)) {
                    out.put(r.getArg(0), in.get(r.getArg(2)));
                }
            } else if (rightOp instanceof InstanceInvokeExpr
                    || rightOp instanceof InterfaceInvokeExpr) {
                InstanceInvokeExpr r = (InstanceInvokeExpr) rightOp;
                String methodName = r.getMethod().getName();

                Type type = r.getBase().getType();

                //   System.out.println("baseType = " + type);
                //  System.out.println("methodName = " + methodName);
                if (type instanceof NullType) {
                    // Note: The control path that causes this to be
                    // null should never occur in practice.
                    return;
                }

                SootClass baseClass = ((RefType) type).getSootClass();

                // FIXME: match better.
                // If we are invoking a method on a token, then...
                if (SootUtilities.derivesFrom(baseClass,
                        PtolemyUtilities.tokenClass)) {
                    if (methodName.equals("one") || methodName.equals("zero")
                            || methodName.equals("not")
                            || methodName.equals("bitwiseNot")
                            || methodName.equals("leftShift")
                            || methodName.equals("rightShift")
                            || methodName.equals("logicalRightShift")
                            || methodName.equals("pow")) {
                        // The returned type must be equal to the type
                        // we are calling the method on.
                        _updateTypeInAssignment(leftOp, in.get(r.getBase()),
                                out);
                    } else if (methodName.equals("add")
                            || methodName.equals("addReverse")
                            || methodName.equals("subtract")
                            || methodName.equals("subtractReverse")
                            || methodName.equals("multiply")
                            || methodName.equals("multiplyReverse")
                            || methodName.equals("divide")
                            || methodName.equals("divideReverse")
                            || methodName.equals("modulo")
                            || methodName.equals("moduloReverse")
                            || methodName.equals("bitwiseAnd")
                            || methodName.equals("bitwiseOr")
                            || methodName.equals("bitwiseXor")) {
                        //                         System.out.println("methodName = " + methodName);
                        //                         System.out.println("r.getBase() = " + r.getBase());
                        //                         System.out.println("r.getArg(0) = " + r.getArg(0));
                        //                         System.out.println("type(r.getBase()) = " + in.get(r.getBase()));
                        //                         System.out.println("type(r.getArg(0)) = " + in.get(r.getArg(0)));
                        ptolemy.data.type.Type baseType = (ptolemy.data.type.Type) in
                                .get(r.getBase());
                        ptolemy.data.type.Type argType = (ptolemy.data.type.Type) in
                                .get(r.getArg(0));

                        if ((baseType == null) || (argType == null)) {
                            out.put(leftOp, null);
                        } else {
                            _updateTypeInAssignment(leftOp, TypeLattice
                                    .lattice().leastUpperBound(baseType,
                                            argType), out);
                        }
                    } else if (methodName.equals("convert")) {
                        // The return rightOp type is equal to the base type.
                        // The first argument type is less than or equal to the base type.
                        _updateTypeInAssignment(leftOp, in.get(r.getBase()),
                                out);
                    } else if (methodName.equals("getElement")
                            || methodName.equals("arrayValue")) {
                        ptolemy.data.type.Type arrayType = (ptolemy.data.type.Type) in
                                .get(r.getBase());

                        if ((arrayType != null)
                                && arrayType instanceof ArrayType) {
                            _updateTypeInAssignment(leftOp,
                                    ((ArrayType) arrayType).getElementType(),
                                    out);
                        }
                    } else if (methodName.equals("getElementAsToken")) {
                        ptolemy.data.type.Type matrixType = (ptolemy.data.type.Type) in
                                .get(r.getBase());

                        if ((matrixType != null)
                                && matrixType instanceof MatrixType) {
                            _updateTypeInAssignment(leftOp,
                                    ((MatrixType) matrixType).getElementType(),
                                    out);
                        }
                    } else if (methodName.equals("absolute")) {
                        // Return the same as the input type, unless complex,
                        // in which case, return double.
                        ptolemy.data.type.Type inType = (ptolemy.data.type.Type) in
                                .get(r.getBase());

                        if (inType.equals(BaseType.COMPLEX)) {
                            _updateTypeInAssignment(leftOp, BaseType.DOUBLE,
                                    out);
                        } else {
                            _updateTypeInAssignment(leftOp, inType, out);
                        }
                    }
                } else if (SootUtilities.derivesFrom(baseClass,
                        PtolemyUtilities.componentPortClass)) {
                    // If we are invoking a method on a port.
                    TypedIOPort port = (TypedIOPort) _namedObjAnalysis
                            .getObject((Local) r.getBase());

                    //System.out.println("port for " + r.getBase() + " = " + port);
                    if (methodName.equals("broadcast")) {
                        // The type of the argument must be less than the
                        // type of the port.
                    } else if (methodName.equals("get")) {
                        // The port here may be null if the model does not
                        // actually contain the port...  This happens, for
                        // instance, in MathFunction
                        if (port != null) {
                            _updateTypeInAssignment(leftOp, port.getType(), out);
                        }
                    } else if (methodName.equals("send")) {
                        if (r.getArgCount() == 3) {
                            // The type of the argument must be less than the
                            // type of the port.
                            //r.getArg(1));
                        } else if (r.getArgCount() == 2) {
                            // The type of the argument must be less than the
                            // type of the port.
                            //            r.getArg(1));
                        }
                    }
                } else if (SootUtilities.derivesFrom(baseClass,
                        PtolemyUtilities.attributeClass)) {
                    // If we are invoking a method on a parameter.
                    Attribute attribute = (Attribute) _namedObjAnalysis
                            .getObject((Local) r.getBase());

                    if (attribute == null) {
                        // A method invocation with a null base is bogus,
                        // so don't create a type constraint.
                    }
View Full Code Here

TOP

Related Classes of soot.jimple.InstanceInvokeExpr

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.