Package soot.jimple

Examples of soot.jimple.JimpleBody


            PortInliner inliner) {
        // Loop through all the methods and inline calls on ports.
        for (Iterator methods = modelClass.getMethods().iterator(); methods
                .hasNext();) {
            SootMethod method = (SootMethod) methods.next();
            JimpleBody body = (JimpleBody) method.retrieveActiveBody();

            if (_debug) {
                System.out.println("inline inside port body of " + method
                        + " = " + body);
            }

            boolean moreToDo = true;

            while (moreToDo) {
                moreToDo = _inlineInsideMethodCalls(modelClass, model, method,
                        body, inliner, _debug);
                LocalNameStandardizer.v().transform(body, _phaseName + ".lns");
            }
        }

        // Loop over all the model instance classes.
        for (Iterator entities = model.deepEntityList().iterator(); entities
                .hasNext();) {
            ComponentEntity entity = (ComponentEntity) entities.next();
            String className = ModelTransformer.getInstanceClassName(entity,
                    _options);
            SootClass entityClass = Scene.v().loadClassAndSupport(className);

            // Loop through all the methods and replace calls on ports.
            for (Iterator methods = entityClass.getMethods().iterator(); methods
                    .hasNext();) {
                SootMethod method = (SootMethod) methods.next();
                JimpleBody body = (JimpleBody) method.retrieveActiveBody();

                //System.out.println("Replacing port invocations in" + method);
                // System.out.println("method = " + method);
                boolean moreToDo = true;
View Full Code Here


        {
            System.out.println("creating <init>");

            // Populate the initialization method.
            JimpleBody body = Jimple.v().newBody(initMethod);
            initMethod.setActiveBody(body);
            body.insertIdentityStmts();

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

            // Populate...
            // Initialize attributes that already exist in the class.
            //  System.out.println("initializing attributes");
            ModelTransformer.createAttributes(body, entity, thisLocal, entity,
                    thisLocal, entityInstanceClass, tempCreatedMap);

            // Create and initialize ports
            // System.out.println("initializing ports");
            ModelTransformer.createPorts(body, thisLocal, entity, thisLocal,
                    entity, entityInstanceClass, tempCreatedMap);

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

        // Add fields to contain the tokens for each port.
        Map nameToField = new HashMap();
        Map nameToType = new HashMap();

        {
            Iterator inputPorts = entity.inputPortList().iterator();

            while (inputPorts.hasNext()) {
                TypedIOPort port = (TypedIOPort) (inputPorts.next());
                String name = port.getName(entity);
                Type type = PtolemyUtilities.tokenType;
                nameToType.put(name, port.getType());

                // PtolemyUtilities.getSootTypeForTokenType(
                //  port.getType());
                SootField field = new SootField(StringUtilities
                        .sanitizeName(name)
                        + "Token", type);
                entityInstanceClass.addField(field);
                nameToField.put(name, field);

                field = new SootField(StringUtilities.sanitizeName(name)
                        + "IsPresent", type);
                entityInstanceClass.addField(field);
                nameToField.put(name + "_isPresent", field);
            }
        }

        {
            SootMethod preinitializeMethod = new SootMethod("preinitialize",
                    Collections.EMPTY_LIST, VoidType.v(), Modifier.PUBLIC);
            entityInstanceClass.addMethod(preinitializeMethod);

            JimpleBody body = Jimple.v().newBody(preinitializeMethod);
            preinitializeMethod.setActiveBody(body);
            body.insertIdentityStmts();

            Stmt insertPoint = Jimple.v().newReturnVoidStmt();
            body.getUnits().add(insertPoint);
            ModelTransformer.initializeAttributesBefore(body, insertPoint,
                    entity, body.getThisLocal(), entity, body.getThisLocal(),
                    entityInstanceClass);
        }

        // Add a field to keep track of the current state.
        SootField currentStateField = new SootField("_currentState", IntType
                .v());
        entityInstanceClass.addField(currentStateField);

        SootField nextTransitionField = new SootField("_nextTransition",
                IntType.v());
        entityInstanceClass.addField(nextTransitionField);
        // populate the initialize method.
        {
            System.out.println("create initialize()");

            SootMethod initializeMethod = new SootMethod("initialize",
                    Collections.EMPTY_LIST, VoidType.v(), Modifier.PUBLIC);
            entityInstanceClass.addMethod(initializeMethod);

            JimpleBody body = Jimple.v().newBody(initializeMethod);
            initializeMethod.setActiveBody(body);
            body.insertIdentityStmts();

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

            // Set the initial state.
            String initialStateName = ((StringAttribute) entity
                    .getAttribute("initialStateName")).getExpression();
            int initialStateIndex = entity.entityList().indexOf(
                    entity.getEntity(initialStateName));
            units.add(Jimple.v().newAssignStmt(
                    Jimple.v().newInstanceFieldRef(thisLocal,
                            currentStateField.makeRef()),
                    IntConstant.v(initialStateIndex)));

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

            LocalNameStandardizer.v().transform(body, "at.lns");
            LocalSplitter.v().transform(body, "at.ls");
        }
        // populate the fire method.
        {
            System.out.println("create fire()");

            SootMethod fireMethod = new SootMethod("fire",
                    Collections.EMPTY_LIST, VoidType.v(), Modifier.PUBLIC);
            entityInstanceClass.addMethod(fireMethod);

            JimpleBody body = Jimple.v().newBody(fireMethod);
            fireMethod.setActiveBody(body);
            body.insertIdentityStmts();

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

            Local hasTokenLocal = Jimple.v().newLocal("hasTokenLocal",
                    BooleanType.v());
            body.getLocals().add(hasTokenLocal);

            Local tokenLocal = Jimple.v().newLocal("tokenLocal",
                    PtolemyUtilities.tokenType);
            body.getLocals().add(tokenLocal);

            Iterator inputPorts = entity.inputPortList().iterator();

            while (inputPorts.hasNext()) {
                TypedIOPort port = (TypedIOPort) (inputPorts.next());

                // FIXME: Handle multiports
                if (port.getWidth() > 0) {
                    String name = port.getName(entity);

                    // Create an if statement.
                    //
                    Local portLocal = Jimple.v().newLocal("port",
                            PtolemyUtilities.componentPortType);
                    body.getLocals().add(portLocal);

                    SootField portField = entityInstanceClass
                            .getFieldByName(StringUtilities.sanitizeName(name));
                    units.add(Jimple.v().newAssignStmt(
                            portLocal,
                            Jimple.v().newInstanceFieldRef(thisLocal,
                                    portField.makeRef())));
                    units.add(Jimple.v().newAssignStmt(
                            hasTokenLocal,
                            Jimple.v().newVirtualInvokeExpr(portLocal,
                                    PtolemyUtilities.hasTokenMethod.makeRef(),
                                    IntConstant.v(0))));

                    Local hasTokenToken = PtolemyUtilities.addTokenLocal(body,
                            "token", PtolemyUtilities.booleanTokenClass,
                            PtolemyUtilities.booleanTokenConstructor,
                            hasTokenLocal);

                    // store the isPresent
                    SootField tokenIsPresentField = (SootField) nameToField
                            .get(name + "_isPresent");
                    units.add(Jimple.v().newAssignStmt(
                            Jimple.v().newInstanceFieldRef(thisLocal,
                                    tokenIsPresentField.makeRef()),
                            hasTokenToken));

                    Stmt target = Jimple.v().newNopStmt();
                    units.add(Jimple.v().newIfStmt(
                            Jimple.v().newEqExpr(hasTokenLocal,
                                    IntConstant.v(0)), target));
                    units.add(Jimple.v().newAssignStmt(
                            tokenLocal,
                            Jimple.v().newVirtualInvokeExpr(portLocal,
                                    PtolemyUtilities.getMethod.makeRef(),
                                    IntConstant.v(0))));

                    SootField tokenField = (SootField) nameToField.get(name);
                    units.add(Jimple.v().newAssignStmt(
                            Jimple.v().newInstanceFieldRef(thisLocal,
                                    tokenField.makeRef()), tokenLocal));
                    units.add(target);
                }
            }

            Map stateToStartStmt = new HashMap();
            List stateStmtList = new LinkedList();
            int numberOfStates = entity.entityList().size();

            // Figure out what state we are in.
            for (Iterator states = entity.entityList().iterator(); states
                    .hasNext();) {
                State state = (State) states.next();
                Stmt startStmt = Jimple.v().newNopStmt();

                stateToStartStmt.put(state, startStmt);
                stateStmtList.add(startStmt);
            }

            Local currentStateLocal = Jimple.v().newLocal("currentStateLocal",
                    IntType.v());
            body.getLocals().add(currentStateLocal);

            Local nextStateLocal = Jimple.v().newLocal("nextStateLocal",
                    IntType.v());
            body.getLocals().add(nextStateLocal);

            Local flagLocal = Jimple.v().newLocal("flagLocal", BooleanType.v());
            body.getLocals().add(flagLocal);

            Local transitionTakenLocal = Jimple.v().newLocal(
                    "transitionTakenLocal", BooleanType.v());
            body.getLocals().add(transitionTakenLocal);

            Local nextTransitionLocal = Jimple.v().newLocal(
                    "nextTransitionLocal", IntType.v());
            body.getLocals().add(nextTransitionLocal);

            units.add(Jimple.v().newAssignStmt(
                    currentStateLocal,
                    Jimple.v().newInstanceFieldRef(thisLocal,
                            currentStateField.makeRef())));

            // Start by doing nothing.
            units.add(Jimple.v().newAssignStmt(transitionTakenLocal,
                    IntConstant.v(0)));
            units.add(Jimple.v().newAssignStmt(nextTransitionLocal,
                    IntConstant.v(-1)));

            // If no transition is taken, then stay in this state.
            units.add(Jimple.v().newAssignStmt(nextStateLocal,
                    currentStateLocal));

            Stmt finishedStmt = Jimple.v().newNopStmt();
            Stmt errorStmt = Jimple.v().newNopStmt();

            // Get the current state.
            units.add(Jimple.v().newTableSwitchStmt(currentStateLocal, 0,
                    numberOfStates - 1, stateStmtList, errorStmt));

            // Generate code for each state.
            for (Iterator states = entity.entityList().iterator(); states
                    .hasNext();) {
                State state = (State) states.next();
                System.out.println("state " + state.getName());

                Stmt startStmt = (Stmt) stateToStartStmt.get(state);
                units.add(startStmt);

                // Fire the refinement actor.
                TypedActor[] refinements = null;

                try {
                    refinements = state.getRefinement();
                } catch (Exception ex) {
                    throw new RuntimeException(ex.getMessage());
                }

                if (refinements != null) {
                    for (int i = 0; i < refinements.length; i++) {
                        TypedActor refinement = refinements[i];

                        Local containerLocal = Jimple.v().newLocal("container",
                                RefType.v(PtolemyUtilities.namedObjClass));
                        body.getLocals().add(containerLocal);

                        Local entityLocal = Jimple.v().newLocal("entity",
                                RefType.v(PtolemyUtilities.entityClass));
                        body.getLocals().add(entityLocal);

                        NamedObj containerModel = entity.getContainer();
                        String deepName = ((NamedObj) refinement)
                                .getName(containerModel);

                        units.add(Jimple.v().newAssignStmt(
                                containerLocal,
                                Jimple.v().newInterfaceInvokeExpr(
                                        thisLocal,
                                        PtolemyUtilities.getContainerMethod
                                                .makeRef())));
                        units
                                .add(Jimple
                                        .v()
                                        .newAssignStmt(
                                                containerLocal,
                                                Jimple
                                                        .v()
                                                        .newCastExpr(
                                                                containerLocal,
                                                                RefType
                                                                        .v(PtolemyUtilities.compositeActorClass))));
                        units.add(Jimple.v().newAssignStmt(
                                entityLocal,
                                Jimple.v().newVirtualInvokeExpr(
                                        containerLocal,
                                        PtolemyUtilities.getEntityMethod
                                                .makeRef(),
                                        StringConstant.v(deepName))));

                        units
                                .add(Jimple
                                        .v()
                                        .newAssignStmt(
                                                entityLocal,
                                                Jimple
                                                        .v()
                                                        .newCastExpr(
                                                                entityLocal,
                                                                RefType
                                                                        .v(PtolemyUtilities.compositeActorClass))));

                        SootMethod rprefireMethod;
                        SootMethod rfireMethod;
                        SootMethod rpostfireMethod;

                        if (refinement instanceof CompositeActor) {
                            rprefireMethod = SootUtilities
                                    .searchForMethodByName(
                                            PtolemyUtilities.compositeActorClass,
                                            "prefire");
                            rfireMethod = SootUtilities.searchForMethodByName(
                                    PtolemyUtilities.compositeActorClass,
                                    "fire");
                            rpostfireMethod = SootUtilities
                                    .searchForMethodByName(
                                            PtolemyUtilities.compositeActorClass,
                                            "postfire");
                        } else {
                            throw new RuntimeException();
                        }

                        units.add(Jimple.v().newInvokeStmt(
                                Jimple.v().newVirtualInvokeExpr(entityLocal,
                                        rprefireMethod.makeRef())));
                        units.add(Jimple.v().newInvokeStmt(
                                Jimple.v().newVirtualInvokeExpr(entityLocal,
                                        rfireMethod.makeRef())));
                        units.add(Jimple.v().newInvokeStmt(
                                Jimple.v().newVirtualInvokeExpr(entityLocal,
                                        rpostfireMethod.makeRef())));
                    }
                }

                // Determine the next state in this state.
                for (Iterator transitions = state.outgoingPort
                        .linkedRelationList().iterator(); transitions.hasNext();) {
                    Transition transition = (Transition) transitions.next();
                    System.out.println("transition = " + transition);

                    String guardExpression = transition.getGuardExpression();

                    Local guardLocal = DataUtilities.generateExpressionCode(
                            entity, entityInstanceClass, guardExpression,
                            nameToField, nameToType, body);

                    // Test the guard.
                    units
                            .add(Jimple
                                    .v()
                                    .newAssignStmt(
                                            tokenLocal,
                                            Jimple
                                                    .v()
                                                    .newCastExpr(
                                                            guardLocal,
                                                            RefType
                                                                    .v(PtolemyUtilities.booleanTokenClass))));
                    units.add(Jimple.v().newAssignStmt(
                            flagLocal,
                            Jimple.v().newVirtualInvokeExpr(
                                    tokenLocal,
                                    PtolemyUtilities.booleanValueMethod
                                            .makeRef())));

                    Stmt skipStmt = Jimple.v().newNopStmt();

                    units.add(Jimple.v().newIfStmt(
                            Jimple.v().newEqExpr(flagLocal, IntConstant.v(0)),
                            skipStmt));
                    units.add(Jimple.v().newIfStmt(
                            Jimple.v().newEqExpr(transitionTakenLocal,
                                    IntConstant.v(1)), errorStmt));

                    // If transition taken, then store the next state
                    units.add(Jimple.v().newAssignStmt(transitionTakenLocal,
                            IntConstant.v(1)));
                    units.add(Jimple.v().newAssignStmt(
                            nextTransitionLocal,
                            IntConstant.v(entity.relationList().indexOf(
                                    transition))));

                    int nextStateIndex = entity.entityList().indexOf(
                            transition.destinationState());
                    units.add(Jimple.v().newAssignStmt(nextStateLocal,
                            IntConstant.v(nextStateIndex)));

                    // Generate code for the outputExpression of the guard.
                    for (Iterator actions = transition.choiceActionList()
                            .iterator(); actions.hasNext();) {
                        AbstractActionsAttribute action = (AbstractActionsAttribute) actions
                                .next();
                        System.out.println("action = " + action);
                        _generateActionCode(entity, entityInstanceClass,
                                nameToField, nameToType, body, action);
                    }

                    units.add(skipStmt);
                }

                units.add(Jimple.v().newGotoStmt(finishedStmt));
            }

            units.add(errorStmt);

            // throw an exception.
            units.add(finishedStmt);

            Local exceptionLocal = SootUtilities.createRuntimeException(body,
                    errorStmt, "state error");
            units.insertBefore(Jimple.v().newThrowStmt(exceptionLocal),
                    errorStmt);

            // Store the next state.
            units.add(Jimple.v().newAssignStmt(
                    Jimple.v().newInstanceFieldRef(thisLocal,
                            currentStateField.makeRef()), nextStateLocal));

            // And the next Transition.
            units.add(Jimple.v()
                    .newAssignStmt(
                            Jimple.v().newInstanceFieldRef(thisLocal,
                                    nextTransitionField.makeRef()),
                            nextTransitionLocal));

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

            LocalNameStandardizer.v().transform(body, "at.lns");
            LocalSplitter.v().transform(body, "at.ls");
        }
        // populate the postfire method.
        {
            System.out.println("create postfire()");

            SootMethod postfireMethod = new SootMethod("postfire",
                    Collections.EMPTY_LIST, BooleanType.v(), Modifier.PUBLIC);
            entityInstanceClass.addMethod(postfireMethod);

            JimpleBody body = Jimple.v().newBody(postfireMethod);
            postfireMethod.setActiveBody(body);
            body.insertIdentityStmts();

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

            Map transitionToStartStmt = new HashMap();
            List transitionStmtList = new LinkedList();
            int numberOfTransitions = entity.relationList().size();

            // Figure out what transition we are in.
            for (Iterator transitions = entity.relationList().iterator(); transitions
                    .hasNext();) {
                Transition transition = (Transition) transitions.next();
                Stmt startStmt = Jimple.v().newNopStmt();

                transitionToStartStmt.put(transition, startStmt);
                transitionStmtList.add(startStmt);
            }

            Local nextTransitionLocal = Jimple.v().newLocal(
                    "nextTransitionLocal", IntType.v());
            body.getLocals().add(nextTransitionLocal);

            units.add(Jimple.v().newAssignStmt(
                    nextTransitionLocal,
                    Jimple.v().newInstanceFieldRef(thisLocal,
                            nextTransitionField.makeRef())));

            Stmt finishedStmt = Jimple.v().newNopStmt();
            Stmt errorStmt = Jimple.v().newNopStmt();

            // Get the current transition..
            units.add(Jimple.v().newTableSwitchStmt(nextTransitionLocal, 0,
                    numberOfTransitions - 1, transitionStmtList, errorStmt));

            // Generate code for each transition
            for (Iterator transitions = entity.relationList().iterator(); transitions
                    .hasNext();) {
                Transition transition = (Transition) transitions.next();
                Stmt startStmt = (Stmt) transitionToStartStmt.get(transition);
                units.add(startStmt);

                // Generate code for the commitExpression of the guard.
                for (Iterator actions = transition.commitActionList()
                        .iterator(); actions.hasNext();) {
                    AbstractActionsAttribute action = (AbstractActionsAttribute) actions
                            .next();
                    _generateActionCode(entity, entityInstanceClass,
                            nameToField, nameToType, body, action);
                }

                // Generate code to reinitialize the target state, if
                // reset is true.
                TypedActor[] refinements = null;

                try {
                    BooleanToken resetToken = (BooleanToken) transition.reset
                            .getToken();

                    if (resetToken.booleanValue()) {
                        refinements = (transition.destinationState())
                                .getRefinement();
                    }
                } catch (Exception ex) {
                    throw new RuntimeException(ex.getMessage());
                }

                if (refinements != null) {
                    for (int i = 0; i < refinements.length; i++) {
                        TypedActor refinement = refinements[i];

                        Local containerLocal = Jimple.v().newLocal("container",
                                RefType.v(PtolemyUtilities.namedObjClass));
                        body.getLocals().add(containerLocal);

                        Local entityLocal = Jimple.v().newLocal("entity",
                                RefType.v(PtolemyUtilities.entityClass));
                        body.getLocals().add(entityLocal);

                        NamedObj containerModel = entity.getContainer();
                        String deepName = ((NamedObj) refinement)
                                .getName(containerModel);
View Full Code Here

        SootMethod functionConstructor = new SootMethod("<init>",
                argumentSootTypes, VoidType.v(), Modifier.PUBLIC);
        functionClass.addMethod(functionConstructor);

        {
            JimpleBody body = Jimple.v().newBody(functionConstructor);
            functionConstructor.setActiveBody(body);
            body.insertIdentityStmts();
            body.getUnits().add(
                    Jimple.v()
                            .newInvokeStmt(
                                    Jimple.v().newSpecialInvokeExpr(
                                            body.getThisLocal(),
                                            PtolemyUtilities.objectConstructor
                                                    .makeRef(),
                                            Collections.EMPTY_LIST)));

            // Read the parameters to the closure constructor.
            int i = 0;

            for (Iterator freeVariables = freeVariableList.iterator(); freeVariables
                    .hasNext();) {
                String freeVariable = (String) freeVariables.next();
                Local local = body.getParameterLocal(i);
                SootField field = (SootField) nameToLocal.get(freeVariable);
                body.getUnits().add(
                        Jimple.v().newAssignStmt(
                                Jimple.v().newInstanceFieldRef(
                                        body.getThisLocal(), field.makeRef()),
                                local));
                i++;
            }

            body.getUnits().add(Jimple.v().newReturnVoidStmt());
        }
        // Create the apply method.
        {
            List argTypes = new LinkedList();
            argTypes.add(ArrayType.v(PtolemyUtilities.tokenType, 1));

            SootMethod functionApplyMethod = new SootMethod("apply", argTypes,
                    PtolemyUtilities.tokenType, Modifier.PUBLIC);
            functionClass.addMethod(functionApplyMethod);

            JimpleBody body = Jimple.v().newBody(functionApplyMethod);
            functionApplyMethod.setActiveBody(body);
            body.insertIdentityStmts();

            Stmt insertPoint = Jimple.v().newNopStmt();
            body.getUnits().add(insertPoint);

            // Loop over all the arguments and populate the map from
            // identifier to local.
            List list = node.getArgumentNameList();
            Local paramLocal = body.getParameterLocal(0);
            int count = 0;

            for (Iterator names = list.iterator(); names.hasNext(); count++) {
                String name = (String) names.next();
                Local argLocal = Jimple.v().newLocal("argument_" + name,
                        RefType.v(PtolemyUtilities.tokenClass));
                body.getLocals().add(argLocal);
                body.getUnits().insertBefore(
                        Jimple.v().newAssignStmt(
                                argLocal,
                                Jimple.v().newArrayRef(paramLocal,
                                        IntConstant.v(count))), insertPoint);
                nameToLocal.put(name, argLocal);
                nameToType.put(name, node.getArgumentTypes()[count]);
            }

            // FIXME: bound arguments?
            DataUtilities.ActorCodeGenerationScope scope = new DataUtilities.ActorCodeGenerationScope(
                    null, null, nameToLocal, nameToType, body, insertPoint);
            ParseTreeCodeGenerator generator = new ParseTreeCodeGenerator();
            Local local = generator.generateCode(cloneTree, body, insertPoint,
                    scope);

            body.getUnits().add(Jimple.v().newReturnStmt(local));
        }
        // Create the getNumberOfArguments method.
        {
            List argTypes = new LinkedList();

            SootMethod getNumberOfArgumentsMethod = new SootMethod(
                    "getNumberOfArguments", argTypes, IntType.v(),
                    Modifier.PUBLIC);
            functionClass.addMethod(getNumberOfArgumentsMethod);

            JimpleBody body = Jimple.v().newBody(getNumberOfArgumentsMethod);
            getNumberOfArgumentsMethod.setActiveBody(body);
            body.insertIdentityStmts();
            body.getUnits().add(
                    Jimple.v().newReturnStmt(
                            IntConstant.v(functionType.getArgCount())));
        }
        // Create the isCongruent method.
        {
            List argTypes = new LinkedList();
            argTypes.add(RefType.v(PtolemyUtilities.functionInterface));

            SootMethod isCongruentMethod = new SootMethod("isCongruent",
                    argTypes, BooleanType.v(), Modifier.PUBLIC);
            functionClass.addMethod(isCongruentMethod);

            JimpleBody body = Jimple.v().newBody(isCongruentMethod);
            isCongruentMethod.setActiveBody(body);
            body.insertIdentityStmts();

            // Never congruent
            // FIXME: really this should compare the string values.
            body.getUnits().add(Jimple.v().newReturnStmt(IntConstant.v(0)));
        }
        // Create the toString method.
        {
            SootMethod toStringMethod = new SootMethod("toString",
                    Collections.EMPTY_LIST, RefType.v("java.lang.String"),
                    Modifier.PUBLIC);
            functionClass.addMethod(toStringMethod);

            JimpleBody body = Jimple.v().newBody(toStringMethod);
            toStringMethod.setActiveBody(body);
            body.insertIdentityStmts();

            StringBuffer buffer = new StringBuffer("(function(");
            int n = node.getArgumentNameList().size();

            for (int i = 0; i < n; i++) {
                if (i > 0) {
                    buffer.append(", ");
                }

                buffer.append((String) node.getArgumentNameList().get(i));

                ptolemy.data.type.Type argType = node.getArgumentTypes()[i];

                if (argType != BaseType.GENERAL) {
                    buffer.append(":");
                    buffer.append(argType.toString());
                }
            }

            buffer.append(") ");

            // FIXME: really this should include runtime references to f and g.
            ParseTreeWriter writer = new ParseTreeWriter();
            String string = writer.printParseTree(node.getExpressionTree());
            buffer.append(string);
            buffer.append(")");

            body.getUnits().add(
                    Jimple.v().newReturnStmt(
                            StringConstant.v(buffer.toString())));
        }

        // Now instantiate the function class.
View Full Code Here

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

                // System.out.println("method = " + method);
                JimpleBody body = (JimpleBody) method.retrieveActiveBody();

                for (Iterator traps = body.getTraps().iterator(); traps
                        .hasNext();) {
                    Trap trap = (Trap) traps.next();
                    SootClass exception = trap.getException();

                    if (_isPtolemyException(exception)) {
View Full Code Here

                            // Keep track of the modified constructor.
                            classToConstructorMap.put(theClass, method);

                            // Replace the parameter refs so THEY have
                            // the right type, too..
                            JimpleBody body = (JimpleBody) method
                                    .retrieveActiveBody();

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

                                if (unit instanceof IdentityStmt) {
                                    IdentityStmt identityStmt = (IdentityStmt) unit;
                                    Value value = identityStmt.getRightOp();

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

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

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

        // Reset the hierarchy, since we've changed superclasses and such.
        Scene.v().setActiveHierarchy(new Hierarchy());
        Scene.v().setFastHierarchy(new FastHierarchy());

        // Fix the specialInvokes.
        for (Iterator i = Scene.v().getApplicationClasses().iterator(); i
                .hasNext();) {

            SootClass theClass = (SootClass) i.next();
            // Loop through all the methods in the class.
            for (Iterator methods = theClass.getMethods().iterator(); methods
                    .hasNext();) {
                SootMethod method = (SootMethod) methods.next();

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

                for (Iterator units = body.getUnits().snapshotIterator(); units
                        .hasNext();) {
                    Stmt unit = (Stmt) units.next();
                    if (unit.containsInvokeExpr()) {
                        ValueBox box = unit.getInvokeExprBox();
                        Value value = box.getValue();
View Full Code Here

        // not really sure what this does..
        Scene.v().setMainClass(modelClass);

        // Initialize the model.
        SootMethod initMethod = modelClass.getInitMethod();
        JimpleBody body = Jimple.v().newBody(initMethod);
        initMethod.setActiveBody(body);
        body.insertIdentityStmts();

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

        _entityLocalMap = new HashMap();
        _portLocalMap = new HashMap();

        // Now instantiate all the stuff inside the model.
View Full Code Here

    private static void _removeSuperExecutableMethods(SootClass theClass) {
        // Loop through all the methods
        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();) {
                Unit unit = (Unit) units.next();
                Iterator boxes = unit.getUseBoxes().iterator();

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

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

                        if (PtolemyUtilities.executableInterface
                                .declaresMethod(r.getMethod().getSubSignature())) {
                            if (r.getMethod().getName().equals("prefire")
                                    || r.getMethod().getName().equals(
                                            "postfire")) {
                                box.setValue(IntConstant.v(1));
                            } else {
                                body.getUnits().remove(unit);
                            }
                        } else if (!r.getMethod().getName().equals("<init>")) {
                            System.out.println("superCall:" + r);
                        }
                    }
View Full Code Here

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

            //       System.out.println("replaceAttributeCalls in " + method);
            JimpleBody body = (JimpleBody) method.retrieveActiveBody();

            CompleteUnitGraph unitGraph = new CompleteUnitGraph(body);

            // this will help us figure out where locals are defined.
            SimpleLocalDefs localDefs = new SimpleLocalDefs(unitGraph);

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

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

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

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

                    if (r.getMethod().getSubSignature().equals(_getDirectorSig)) {
                        // Replace calls to getDirector with
                        // null.  FIXME: we should be able to
                        // do better than this?
                        if (unit instanceof InvokeStmt) {
                            body.getUnits().remove(unit);
                        } else {
                            box.setValue(NullConstant.v());
                        }
                    } else if (r.getMethod().getSubSignature().equals(
                            _getAttributeSig)) {
                        if (unit instanceof InvokeStmt) {
                            body.getUnits().remove(unit);
                        } else {
                            // Replace calls to getAttribute(arg)
                            // when arg is a string that can be
                            // statically evaluated.
                            Value nameValue = r.getArg(0);
View Full Code Here

            // Loop over all the methods...
            for (Iterator methods = theClass.getMethods().iterator(); methods
                    .hasNext();) {
                SootMethod method = (SootMethod) methods.next();

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

                // Loop over all the statements.
                for (Iterator units = body.getUnits().snapshotIterator(); units
                        .hasNext();) {
                    Stmt unit = (Stmt) units.next();

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

                    ValueBox box = unit.getInvokeExprBox();
                    InvokeExpr r = (InvokeExpr) box.getValue();

                    if (r.getMethod().getSubSignature().equals(
                            PtolemyUtilities.invalidateResolvedTypesMethod
                                    .getSubSignature())) {
                        // Remove calls to invalidateResolvedTypes()
                        body.getUnits().remove(unit);
                    }
                }
            }
        }
    }
View Full Code Here

        // Create references to the buffer for each port channel
        for (Iterator methods = entityClass.getMethods().iterator(); methods
                .hasNext();) {
            SootMethod method = (SootMethod) methods.next();
            JimpleBody body = (JimpleBody) method.retrieveActiveBody();
            Object insertPoint = body.getUnits().getLast();

            // Insert code into all the init methods.
            if (!method.getName().equals("<init>")) {
                continue;
            }

            Local channelLocal = Jimple.v().newLocal("channel",
                    ArrayType.v(tokenType, 1));
            body.getLocals().add(channelLocal);

            // Create the array of port channels.
            body.getUnits().insertBefore(
                    Jimple.v().newAssignStmt(
                            channelLocal,
                            Jimple.v().newNewArrayExpr(tokenType,
                                    IntConstant.v(port.getWidth()))),
                    insertPoint);

            // Set the field to point to the new array.
            body.getUnits().insertBefore(
                    Jimple.v().newAssignStmt(
                            Jimple.v().newInstanceFieldRef(body.getThisLocal(),
                                    bufferField.makeRef()), channelLocal),
                    insertPoint);
        }
    }
View Full Code Here

TOP

Related Classes of soot.jimple.JimpleBody

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.