Examples of JimpleBody


Examples of soot.jimple.JimpleBody

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

Examples of soot.jimple.JimpleBody

        // them to Exceptions, unless they are in constructors,
        // in which case remove them.
        for (Iterator methods = theClass.getMethods().iterator(); methods
                .hasNext();) {
            SootMethod method = (SootMethod) methods.next();
            JimpleBody body = (JimpleBody) method.retrieveActiveBody();
            Chain units = body.getUnits();

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

                // Remove all the definitions.
                for (Iterator boxes = stmt.getDefBoxes().iterator(); boxes
                        .hasNext();) {
                    ValueBox box = (ValueBox) boxes.next();
                    Value value = box.getValue();

                    if (value instanceof FieldRef) {
                        FieldRef ref = (FieldRef) value;

                        if (ref.getField() == theField) {
                            System.out.println("removing stmt = " + stmt);
                            units.remove(stmt);
                        }
                    }
                }

                // Inline all the uses.
                if (Evaluator.isValueConstantValued(newValue)) {
                    for (Iterator boxes = stmt.getUseBoxes().iterator(); boxes
                            .hasNext();) {
                        ValueBox box = (ValueBox) boxes.next();
                        Value value = box.getValue();

                        if (value instanceof FieldRef) {
                            FieldRef ref = (FieldRef) value;

                            if (ref.getField() == theField) {
                                System.out.println("inlining stmt = " + stmt);

                                box.setValue(Evaluator
                                        .getConstantValueOf(newValue));
                            }
                        }
                    }
                }
            }
        }

        if (Modifier.isStatic(theField.getModifiers())) {
            SootMethod method;

            // create a class initializer if one does not already exist.
            if (theClass.declaresMethodByName("<clinit>")) {
                method = theClass.getMethodByName("<clinit>");
            } else {
                method = new SootMethod("<clinit>", new LinkedList(), NullType
                        .v(), Modifier.PUBLIC);
                theClass.addMethod(method);
            }

            JimpleBody body = (JimpleBody) method.retrieveActiveBody();
            Chain units = body.getUnits();
            Stmt insertPoint = (Stmt) units.getLast();
            Local local = Jimple.v().newLocal("_CGTemp" + theField.getName(),
                    theField.getType());
            body.getLocals().add(local);
            units.insertBefore(Jimple.v().newAssignStmt(local, newValue),
                    insertPoint);

            FieldRef fieldRef = Jimple.v()
                    .newStaticFieldRef(theField.makeRef());
            units.insertBefore(Jimple.v().newAssignStmt(fieldRef, local),
                    insertPoint);
        } else {
            for (Iterator methods = theClass.getMethods().iterator(); methods
                    .hasNext();) {
                SootMethod method = (SootMethod) methods.next();

                // ignore things that aren't initializers.
                if (!method.getName().equals("<init>")) {
                    continue;
                }

                JimpleBody body = (JimpleBody) method.retrieveActiveBody();
                Chain units = body.getUnits();
                Stmt insertPoint = (Stmt) units.getLast();
                Local local = Jimple.v().newLocal(
                        "_CGTemp" + theField.getName(), theField.getType());
                body.getLocals().add(local);
                units.insertBefore(Jimple.v().newAssignStmt(local, newValue),
                        insertPoint);

                FieldRef fieldRef = Jimple.v().newInstanceFieldRef(
                        body.getThisLocal(), theField.makeRef());
                units.insertBefore(Jimple.v().newAssignStmt(fieldRef, local),
                        insertPoint);
            }
        }
    }
View Full Code Here

Examples of soot.jimple.JimpleBody

     */
    public String generate(SootMethod method) {
        if (method.isConcrete() && !method.isNative()
                && !OverriddenMethodGenerator.isOverridden(method)) {
            StringBuffer code = new StringBuffer();
            JimpleBody body = (JimpleBody) (method.retrieveActiveBody());
            CSwitch visitor = new CSwitch(_context);

            // For catching exceptions.
            ExceptionTracker tracker = new ExceptionTracker();
            tracker.init(body);
View Full Code Here

Examples of soot.jimple.JimpleBody

     */
    protected String _generateLocal(SootMethod method,
            HashSet parameterAndThisLocals) {
        StringBuffer code = new StringBuffer();

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

        if (body.getLocals().size() > 0) {
            // Declare local variables.
            Iterator locals = body.getLocals().iterator();
            code.append(_indent(1)
                    + _comment("Declarations for local variables."));

            while (locals.hasNext()) {
                Local nextLocal = (Local) (locals.next());

                if (!parameterAndThisLocals.contains(nextLocal)) {
                    code.append(_indent(1));

                    Type localType = nextLocal.getType();
                    code.append(CNames.typeNameOf(localType));
                    code.append(" " + CNames.localNameOf(nextLocal) + ";\n");
                    _updateRequiredTypes(localType);
                }
            }

            code.append("\n");

            // Initialize local variables.
            code.append(_indent(1)
                    + _comment("Initializations for local variables."));

            locals = body.getLocals().iterator();

            while (locals.hasNext()) {
                Local nextLocal = (Local) (locals.next());

                if (!parameterAndThisLocals.contains(nextLocal)) {
View Full Code Here

Examples of soot.jimple.JimpleBody

     @param thisLocalName The local name.
     *  @return The code.
     */
    protected String _generateMethodBody(SootMethod method, CSwitch visitor,
            ExceptionTracker tracker, String thisLocalName) {
        JimpleBody body = (JimpleBody) method.retrieveActiveBody();
        StringBuffer code = new StringBuffer();
        visitor.indentLevel = 0;

        // Generate the method body.
        /*Iterator units = */body.getUnits().iterator();

        if (thisLocalName != null) {
            visitor.setThisLocalName(thisLocalName);
        }

        /*units = */body.getUnits().iterator();

        if (!Context.getSingleClassMode()) {
            code.append(_generateMethodPrologue(tracker, visitor));
        } else {
            visitor.indentLevel = 1;
View Full Code Here

Examples of soot.jimple.JimpleBody

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

            JimpleBody body = Jimple.v().newBody(newMethod);
            body.importBodyContentsFrom(oldMethod.retrieveActiveBody());
            newMethod.setActiveBody(body);
        }

        changeTypesOfFields(newClass, oldClass, newClass);
        changeTypesInMethods(newClass, oldClass, newClass);
View Full Code Here

Examples of soot.jimple.JimpleBody

     @param thisLocalName The local name.
     *  @return The code for the method's declaration(its head).
     */
    protected String _generateMethodDeclaration(SootMethod method,
            HashSet parameterAndThisLocals, String thisLocalName) {
        JimpleBody body = (JimpleBody) method.retrieveActiveBody();
        StringBuffer code = new StringBuffer();
        String description = "Function that implements Method "
                + method.getSignature();
        Type returnType = method.getReturnType();
        code.append(_comment(description));
        code.append(CNames.typeNameOf(returnType));
        _updateRequiredTypes(returnType);
        code.append(" ");
        code.append(CNames.functionNameOf(method));
        code.append("(");

        int parameterIndex;
        int parameterCount = 0;

        if (!method.isStatic()) {
            parameterAndThisLocals.add(body.getThisLocal());
            if (thisLocalName == null) {
                thisLocalName = CNames.localNameOf(body.getThisLocal());
            }
            code.append(CNames.instanceNameOf(method.getDeclaringClass()) + " "
                    + thisLocalName);
            parameterCount++;
        }

        for (parameterIndex = 0; parameterIndex < method.getParameterCount(); parameterIndex++) {
            if (parameterCount++ > 0) {
                code.append(", ");
            }

            Local local = body.getParameterLocal(parameterIndex);
            parameterAndThisLocals.add(local);

            Type parameterType = local.getType();
            code.append(CNames.typeNameOf(parameterType) + " "
                    + CNames.localNameOf(local));
View Full Code Here

Examples of soot.jimple.JimpleBody

        System.out.println("constructor = " + constructorStmt);

        constructorMethod.retrieveActiveBody();

        JimpleBody clinitBody = (JimpleBody) clinitMethod.retrieveActiveBody();
        Chain clinitUnits = clinitBody.getUnits();
        Stmt insertPoint = (Stmt) clinitUnits.getLast();

        // insert a (static) call to the (non static)
        // constructor.
        // Later we will come back and inline this after we make all the
        // method static.
        InvokeExpr constructorExpr = constructorStmt.getInvokeExpr();
        Stmt insertStmt = Jimple.v().newInvokeStmt(
                Jimple.v().newStaticInvokeExpr(
                        staticConstructorMethod.makeRef(),
                        constructorExpr.getArgs()));
        clinitUnits.insertBefore(insertStmt, insertPoint);

        // Loop through the class and make all the non-static method static.
        // Make all reference to this into static references.
        ArrayList methodList = new ArrayList(staticClass.getMethods());

        for (Iterator methods = methodList.iterator(); methods.hasNext();) {
            SootMethod method = (SootMethod) methods.next();

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

            System.out.println("method = " + method);

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

            Local thisLocal = body.getThisLocal();

            // If we have an init method, then remove specialinvoke calls.
            // These are calls to the superclass,
            // and no longer make sense.
            if (method.getName().equals("<init>")) {
                Iterator units = body.getUnits().snapshotIterator();

                while (units.hasNext()) {
                    Stmt s = (Stmt) units.next();

                    if (s instanceof InvokeStmt
                            && ((InvokeStmt) s).getInvokeExpr() instanceof SpecialInvokeExpr) {
                        body.getUnits().remove(s);
                        break;
                    }
                }

                // and rename the method to something that is not reserved
                // so we can call it manually.
                method.setName("_init");
            }

            // FIXME: checks for equality with thisLocal.  What if
            // thisLocal is aliased in another local?  Maybe we should
            // run the CopyPropagator somewhere here?
            // change method calls to static invocation
            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
            // should instead be synchronized on the class.
            for (Iterator stmts = body.getUnits().snapshotIterator(); stmts
                    .hasNext();) {
                Stmt stmt = (Stmt) stmts.next();

                if (stmt instanceof MonitorStmt) {
                    MonitorStmt monitorStmt = (MonitorStmt) stmt;
                    Local lock = (Local) monitorStmt.getOp();

                    if (lock == thisLocal) {
                        Local classLocal = SynchronizerManager.v()
                                .addStmtsToFetchClassBefore(body, stmt);
                        monitorStmt.setOp(classLocal);
                    }
                }
            }

            // remove the this identity statement.
            Iterator units = body.getUnits().snapshotIterator();

            while (units.hasNext()) {
                Stmt s = (Stmt) units.next();

                if (s instanceof IdentityStmt
                        && ((IdentityStmt) s).getRightOp() instanceof ThisRef) {
                    body.getUnits().remove(s);
                }
            }

            // make the method static.
            method.setModifiers(method.getModifiers() | Modifier.STATIC);
View Full Code Here

Examples of soot.jimple.JimpleBody

     *  statement.
     *  @return The code.
     */
    protected String _generateMethodUnitCode(ExceptionTracker tracker,
            CSwitch visitor, SootMethod method, byte indentLevel) {
        JimpleBody body = (JimpleBody) method.retrieveActiveBody();
        StringBuffer code = new StringBuffer();

        //Exception-catching in the body.
        Iterator units = body.getUnits().iterator();
        boolean handle_exceptions = tracker.trapsExist()
                && (!Context.getSingleClassMode());

        while (units.hasNext()) {
            Unit unit = (Unit) (units.next());
View Full Code Here

Examples of soot.jimple.JimpleBody

                // local
                if (method.isStatic()) {
                    continue;
                }

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

                if (debug) {
                    System.out.println("method = " + method);
                }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.