Package soot

Examples of soot.SootMethod


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

                InvokeExpr r = (InvokeExpr) stmt.getInvokeExpr();
                SootMethod targetMethod = r.getMethod();

                // Don't inline obviously recursive methods.
                if (targetMethod == method) {
                    continue;
                }

                // Don't inline methods invoked on the super
                // class. (These get taken care of later explicitly)
                if (!targetMethod.getDeclaringClass().equals(theClass)) {
                    continue;
                }

                boolean isCGInitMethod = targetMethod.getName().equals(
                        "__CGInit");

                // Avoid inlining methods that don't take or return
                // named objects, except for the CGInit method, which
                // is always inlined.
                boolean hasDangerousType = false;

                {
                    Type type = targetMethod.getReturnType();

                    if (type instanceof RefType) {
                        SootClass typeClass = ((RefType) type).getSootClass();

                        if (SootUtilities.derivesFrom(typeClass,
                                PtolemyUtilities.namedObjClass)) {
                            hasDangerousType = true;
                        }
                    }
                }

                for (Iterator argTypes = targetMethod.getParameterTypes()
                        .iterator(); argTypes.hasNext();) {
                    Type type = (Type) argTypes.next();

                    if (type instanceof RefType) {
                        SootClass typeClass = ((RefType) type).getSootClass();
View Full Code Here

        createAttributeComputationFunctions(entity, entity,
                entityInstanceClass, _constAnalysis);

        {
            // create a new body for the initialization method.
            SootMethod initMethod = entityInstanceClass.getInitMethod();
            JimpleBody body = Jimple.v().newBody(initMethod);
            initMethod.setActiveBody(body);
            body.insertIdentityStmts();

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

            // Create a new class for each actor in the model.
            _createActorsIn(entity, tempCreatedMap, "modelTransformer",
                    _constAnalysis, options);

            // Create code in the model class to instantiate the ports
            // and parameters of the model.
            createAttributes(body, entity, thisLocal, entity, thisLocal,
                    entityInstanceClass, tempCreatedMap);

            _ports(body, thisLocal, entity, thisLocal, entity,
                    entityInstanceClass, tempCreatedMap, true);

            // Excess initialization, but necessary for -actor???
            Stmt insertPoint = Jimple.v().newNopStmt();
            body.getUnits().add(insertPoint);

            // InitializeAttributes of the ports and parameters.
            initializeAttributesBefore(body, insertPoint, entity, thisLocal,
                    entity, thisLocal, entityInstanceClass);

            // Create code in the model class to instantiate all
            // actors and relations, and connect the relations
            // to the ports.
            _entities(body, thisLocal, entity, thisLocal, entity,
                    entityInstanceClass, tempCreatedMap, options);
            _relations(body, thisLocal, entity, entityInstanceClass);
            _links(body, entity);
            _linksOnPortsContainedByContainedEntities(body, entity);

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

        implementExecutableInterface(entityInstanceClass);

        // Reinitialize the hierarchy, since we've added classes.
        Scene.v().setActiveHierarchy(new Hierarchy());
        Scene.v().setFastHierarchy(new FastHierarchy());

        {
            Set locallyModifiedAttributeSet = _constAnalysis
                    .getVariablesWithChangeContext(entity);

            // Filter out any attribute that is independent (and might
            // be modified).
            for (Iterator i = locallyModifiedAttributeSet.iterator(); i
                    .hasNext();) {
                if (_constAnalysis.isIndependent((Variable) i.next())) {
                    i.remove();
                }
            }

            // Sort according to dependencies.
            List locallyModifiedAttributeList;

            try {
                DirectedGraph graph = _constAnalysis.getDependencyGraph();
                locallyModifiedAttributeList = Arrays.asList(Graph
                        .weightArray(graph.topologicalSort(graph
                                .nodes(locallyModifiedAttributeSet))));
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }

            System.out.println("locallyModifiedAttributeList of " + entity
                    + " = " + locallyModifiedAttributeList);

            // Add code to the beginning of the prefire method that
            // computes the attribute values of anything that is not a
            // constant.
            SootMethod method = entityInstanceClass.getMethodByName("prefire");
            System.out.println("method = " + method);

            JimpleBody body = (JimpleBody) method.getActiveBody();
            Stmt insertPoint = body.getFirstNonIdentityStmt();
            Local containerLocal = Jimple.v().newLocal("entity",
                    PtolemyUtilities.componentEntityType);
            body.getLocals().add(containerLocal);

            // Invoke the method to compute each attribute, in order.
            for (Iterator attributes = locallyModifiedAttributeList.iterator(); attributes
                    .hasNext();) {
                Attribute attribute = (Attribute) attributes.next();
                Entity attributeContainer = FieldsForEntitiesTransformer
                        .getEntityContainerOfObject(attribute);
                SootClass containerClass = (SootClass) _objectToClassMap
                        .get(attributeContainer);
                SootMethod computeMethod = containerClass
                        .getMethodByName(getAttributeComputationFunctionName(
                                attribute, attributeContainer));
                body.getUnits().insertBefore(
                        Jimple.v().newAssignStmt(
                                containerLocal,
                                Jimple.v().newVirtualInvokeExpr(
                                        body.getThisLocal(),
                                        PtolemyUtilities.getEntityMethod
                                                .makeRef(),
                                        StringConstant.v(attributeContainer
                                                .getName(entity)))),
                        insertPoint);

                // and then cast
                body.getUnits().insertBefore(
                        Jimple.v().newAssignStmt(
                                containerLocal,
                                Jimple.v().newCastExpr(containerLocal,
                                        RefType.v(containerClass))),
                        insertPoint);

                body.getUnits().insertBefore(
                        Jimple.v().newInvokeStmt(
                                Jimple.v().newVirtualInvokeExpr(containerLocal,
                                        computeMethod.makeRef())), insertPoint);
            }

            //    ModelTransformer.computeAttributesBefore(body, insertPoint,
            //                     entity, body.getThisLocal(),
            //                     entity, body.getThisLocal(),
View Full Code Here

        /*Type stringType = */RefType.v("java.lang.String");
        /*Type compositeEntityType = */RefType
                .v("ptolemy.kernel.CompositeEntity");
        /*Type workspaceType = */RefType.v("ptolemy.kernel.util.Workspace");

        _initMethod = new SootMethod("__CGInit", new LinkedList(),
                VoidType.v(), Modifier.PUBLIC);
        addMethod(_initMethod);

        // Now create constructors to call the superclass constructors,
        // and then the __CGInit method.
        for (Iterator methods = getSuperclass().getMethods().iterator(); methods
                .hasNext();) {
            SootMethod method = (SootMethod) methods.next();

            if (!method.getName().equals("<init>")) {
                continue;
            }

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

            // Call the __CGInit method.
            units.add(Jimple.v().newInvokeStmt(
View Full Code Here

    // given method with the same arguments, and then calls the
    // shared _CGInit initialization method.
    private SootMethod _createConstructor(SootClass theClass,
            SootMethod superConstructor) {
        // Create the constructor.
        SootMethod constructor = new SootMethod("<init>", superConstructor
                .getParameterTypes(), superConstructor.getReturnType(),
                superConstructor.getModifiers());

        theClass.addMethod(constructor);

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

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

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

        // get a list of the locals that reference the parameters of the
View Full Code Here

    // Create the communication buffers for communication between
    // actors in the model.
    private void _createBuffers() {
        // First create the circular buffers for communication.
        SootMethod clinitMethod;
        Body clinitBody;

        if (_modelClass.declaresMethodByName("<clinit>")) {
            clinitMethod = _modelClass.getMethodByName("<clinit>");
            clinitBody = clinitMethod.retrieveActiveBody();
        } else {
            clinitMethod = new SootMethod("<clinit>", Collections.EMPTY_LIST,
                    VoidType.v(), Modifier.PUBLIC | Modifier.STATIC);
            _modelClass.addMethod(clinitMethod);
            clinitBody = Jimple.v().newBody(clinitMethod);
            clinitMethod.setActiveBody(clinitBody);
            clinitBody.getUnits().add(Jimple.v().newReturnVoidStmt());
        }

        Chain clinitUnits = clinitBody.getUnits();
View Full Code Here

     * @param v The interface invoke expression.
     */
    public void caseInterfaceInvokeExpr(InterfaceInvokeExpr v) {
        //_generateInstanceInvokeExpression(v);
        //defaultCase(v);
        SootMethod method = v.getMethod();

        v.getBase().apply(this);

        String instanceName = _pop().toString();
        String cast;

        String returnType = CNames.typeNameOf(method.getReturnType());

        if (!method.isStatic()) {
            cast = "(" + returnType + " (*) (void*, ...))";
        } else {
            cast = "(" + returnType + " (*) "
                    + CNames.typeNameOf(method.getParameterType(0)) + ", ...))";
        }

        _push("(" + cast + "(" + instanceName + "->class->lookup("
                + CNames.hashNumberOf(method) + ")))" + "( " + instanceName
                + _generateArguments(v, 1) + ")");
View Full Code Here

    public void caseSpecialInvokeExpr(SpecialInvokeExpr v) {
        // Presently, we consider one case: a call to a method
        // that is declared by the superclass of the base. This
        // occurs when the base class constructor is invoked automatically
        // from with a given class constructor.
        SootMethod method = v.getMethod();

        // Generate cast for first argument of method.
        String cast = "";

        SootClass declaringClass = method.getDeclaringClass();

        if (!declaringClass.isInterface()) {
            cast = "(" + CNames.instanceNameOf(declaringClass)
                    + "/* actual cast */)";
        }

        Iterator inheritedMethods = MethodListGenerator.getInheritedMethods(
                declaringClass).iterator();

        while (inheritedMethods.hasNext()) {
            SootMethod inheritedMethod = (SootMethod) inheritedMethods.next();

            if (inheritedMethod.getSubSignature().equals(
                    method.getSubSignature())) {
                cast = "("
                        + CNames.instanceNameOf(inheritedMethod
                                .getDeclaringClass()) + "/* inherited cast */)";
                break;
            }
        }

View Full Code Here

            _context.addIncludeFile(includeFileName);
        }

        // The method that was invoked.
        SootMethod method = v.getMethod();

        // Handling native methods here.
        _push(CNames.functionNameOf(method) + "(" + _generateArguments(v, 0)
                + ")");
    }
View Full Code Here

     */
    protected String _generateArguments(InvokeExpr expression,
            int previousArguments) {
        StringBuffer code = new StringBuffer();
        Iterator args = expression.getArgs().iterator();
        SootMethod method = expression.getMethod();

        int count = previousArguments;

        while (args.hasNext()) {
            Type expectedParamType = method.getParameterType(count
                    - previousArguments);

            if (count++ > 0) {
                code.append(", ");
            }
View Full Code Here

TOP

Related Classes of soot.SootMethod

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.