Package soot

Examples of soot.RefType


        G.v().out.println("WARNING: Tried to insert guard into "+container+" but couldn't because method has no body.");
      } else {
        Body body = container.getActiveBody();
       
        //exc = new Error
        RefType runtimeExceptionType = RefType.v("java.lang.Error");
        NewExpr newExpr = Jimple.v().newNewExpr(runtimeExceptionType);
        LocalGenerator lg = new LocalGenerator(body);
        Local exceptionLocal = lg.generateLocal(runtimeExceptionType);
        AssignStmt assignStmt = Jimple.v().newAssignStmt(exceptionLocal, newExpr);
        body.getUnits().insertBefore(assignStmt, insertionPoint);
       
        //exc.<init>(message)
        SootMethodRef cref = runtimeExceptionType.getSootClass().getMethod("<init>", Collections.singletonList(RefType.v("java.lang.String"))).makeRef();
        SpecialInvokeExpr constructorInvokeExpr = Jimple.v().newSpecialInvokeExpr(exceptionLocal, cref, StringConstant.v(guard.message));
        InvokeStmt initStmt = Jimple.v().newInvokeStmt(constructorInvokeExpr);
        body.getUnits().insertAfter(initStmt, assignStmt);
       
        if(options.guards().equals("print")) {
View Full Code Here


    AllocNode( PAG pag, Object newExpr, Type t, SootMethod m ) {
  super( pag, t );
        this.method = m;
        if( t instanceof RefType ) {
            RefType rt = (RefType) t;
            if( rt.getSootClass().isAbstract()) {
        boolean usesReflectionLog = new CGOptions(PhaseOptions.v().getPhaseOptions("cg")).reflection_log()!=null;
        if (!usesReflectionLog) {
            throw new RuntimeException( "Attempt to create allocnode with abstract type "+t );
        }
      }
View Full Code Here

    private boolean _replaceTokenInvocation(JimpleBody body, Unit unit,
            ValueBox box, InstanceInvokeExpr r) {
        boolean doneSomething = false;

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

            //System.out.println("baseType = " + type);
            // 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();

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

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

            if (SootUtilities.derivesFrom(type.getSootClass(),
                    PtolemyUtilities.tokenClass)) {
                // if we are invoking a method on a token class, then
                // attempt to get the constant value of the token.
                Token token = getTokenValue((Local) r.getBase(), unit,
                        _localDefs, _tokenAnalysis);
View Full Code Here

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

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

        List modifiedConstructorClassList = new LinkedList();

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

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

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

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

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

    // Return true if the type is one that should not appear in generated
    // code.  This includes Attribute, Settable, Relation, Port, and their
    // subclasses.
    private static boolean _isRemovableType(Type type) {
        if (type instanceof RefType) {
            RefType refType = (RefType) type;
            SootClass refClass = refType.getSootClass();

            if (SootUtilities.derivesFrom(refClass,
                    PtolemyUtilities.attributeClass)
                    || SootUtilities.derivesFrom(refClass,
                            PtolemyUtilities.managerClass)
View Full Code Here

    // Return true if the type is one we will inline because it is a
    // token type.
    private static boolean _isInlineableTokenType(Type type) {
        if (type instanceof RefType) {
            RefType refType = (RefType) type;

            if (SootUtilities.derivesFrom(refType.getSootClass(),
                    PtolemyUtilities.tokenClass)) {
                return true;
            }
        }
View Full Code Here

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

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

                }

                continue;
            }

            RefType type = (RefType) PtolemyUtilities
                    .getBaseTokenType(fieldType);
            SootClass fieldClass = type.getSootClass();

            if (!SootUtilities.derivesFrom(fieldClass,
                    PtolemyUtilities.tokenClass)) {
                if (debug) {
                    System.out.println("Skipping field " + field
View Full Code Here

                // field, then don't do it again.
                if (localToFieldToLocal.get(local) != null) {
                    continue;
                }

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

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

                    continue;
                }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

TOP

Related Classes of soot.RefType

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.