Examples of JimpleBody


Examples of soot.jimple.JimpleBody

            SootClass entityClass = (SootClass) i.next();

            for (Iterator methods = entityClass.getMethods().iterator(); methods
                    .hasNext();) {
                SootMethod method = (SootMethod) methods.next();
                JimpleBody body = (JimpleBody) method.retrieveActiveBody();

                for (Iterator stmts = body.getUnits().iterator(); stmts
                        .hasNext();) {
                    Stmt stmt = (Stmt) stmts.next();

                    for (Iterator boxes = stmt.getUseBoxes().iterator(); boxes
                            .hasNext();) {
                        ValueBox box = (ValueBox) boxes.next();
                        Value value = box.getValue();

                        if (value instanceof FieldRef) {
                            Object field = ((FieldRef) value).getField();
                            unusedFieldSet.remove(field);
                        }
                    }
                }
            }
        }

        // Loop through the methods again, and kill the statements
        // that write to an unused field.
        for (Iterator i = Scene.v().getApplicationClasses().iterator(); i
                .hasNext();) {
            SootClass entityClass = (SootClass) i.next();

            for (Iterator methods = entityClass.getMethods().iterator(); methods
                    .hasNext();) {
                SootMethod method = (SootMethod) methods.next();
                JimpleBody body = (JimpleBody) method.retrieveActiveBody();

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

                    for (Iterator boxes = stmt.getDefBoxes().iterator(); boxes
                            .hasNext();) {
                        ValueBox box = (ValueBox) boxes.next();
                        Value value = box.getValue();

                        if (value instanceof FieldRef) {
                            Object field = ((FieldRef) value).getField();

                            if (unusedFieldSet.contains(field)) {
                                body.getUnits().remove(stmt);
                            }
                        }
                    }
                }
            }
View Full Code Here

Examples of soot.jimple.JimpleBody

            // Don't drag in the bodies of abstract methods.
            if (!method.isConcrete()) {
                continue;
            }

            JimpleBody body = (JimpleBody) method.retrieveActiveBody();
            Scene.v().releaseActiveHierarchy();

            // Grab the types of all traps.
            for (Iterator it = body.getTraps().iterator(); it.hasNext();) {
                Trap t = (Trap) it.next();
                _addClass(t.getException());
            }

            // Grab the classes of all referenced fields, invoked
            // methods, and created classes.
            for (Iterator units = body.getUnits().iterator(); units.hasNext();) {
                Unit unit = (Unit) units.next();

                for (Iterator boxes = unit.getUseAndDefBoxes().iterator(); boxes
                        .hasNext();) {
                    ValueBox box = (ValueBox) boxes.next();
View Full Code Here

Examples of soot.jimple.JimpleBody

    public String getDeclaredOptions() {
        return "targetPackage debug";
    }

    protected void internalTransform(Body b, String phaseName, Map options) {
        JimpleBody body = (JimpleBody) b;

        boolean debug = PhaseOptions.getBoolean(options, "debug");

        if (debug) {
            System.out.println("CastAndInstanceofEliminator.internalTransform("
View Full Code Here

Examples of soot.jimple.JimpleBody

                if (!method.isConcrete()) {
                    continue;
                }

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

                    for (Iterator transformers = _transformers.iterator(); transformers
                            .hasNext();) {
                        BodyTransformer transformer = (BodyTransformer) transformers
                                .next();
View Full Code Here

Examples of soot.jimple.JimpleBody

                if (!m.isConcrete()) {
                    continue;
                }

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

                internalTransform(body, phaseName, options);
            }
        }
    }
View Full Code Here

Examples of soot.jimple.JimpleBody

                if (!instanceInvokesFilter.wrap(cg.edgesOutOf(container))
                        .hasNext()) {
                    continue;
                }

                JimpleBody b = (JimpleBody) container.getActiveBody();

                List unitList = new ArrayList();
                unitList.addAll(b.getUnits());

                Iterator unitIt = unitList.iterator();

                while (unitIt.hasNext()) {
                    Stmt s = (Stmt) unitIt.next();
View Full Code Here

Examples of soot.jimple.JimpleBody

                        // Only look at constructors.
                        if (!initMethod.getName().equals("<init>")) {
                            continue;
                        }

                        JimpleBody initBody = (JimpleBody) initMethod
                                .getActiveBody();
                        Chain initUnits = initBody.getUnits();
                        Local arrayLocal = Jimple.v().newLocal(fieldName,
                                arrayType);
                        initBody.getLocals().add(arrayLocal);

                        // Create the new buffer
                        Stmt insertPoint = initBody.getFirstNonIdentityStmt();

                        // This *should* be the statment after the constructor.
                        insertPoint = (Stmt) initUnits.getSuccOf(insertPoint);

                        Local containerLocal = FieldsForEntitiesTransformer
                                .getLocalReferenceForEntity(_model,
                                        _modelClass, initBody.getThisLocal(),
                                        initBody, insertPoint, _options);

                        initUnits.insertBefore(Jimple.v().newAssignStmt(
                                arrayLocal,
                                Jimple.v().newNewArrayExpr(tokenType,
View Full Code Here

Examples of soot.jimple.JimpleBody

                // Initialize the index fields.
                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 indexesLocal = Jimple.v().newLocal("indexes",
                            ArrayType.v(IntType.v(), 1));
                    body.getLocals().add(indexesLocal);

                    // Initialize the index array field to contain
                    // an array initialized to zero.
                    body.getUnits().insertBefore(
                            Jimple.v().newAssignStmt(
                                    indexesLocal,
                                    Jimple.v().newNewArrayExpr(IntType.v(),
                                            IntConstant.v(port.getWidth()))),
                            insertPoint);

                    // Set the index field to point to the new array
                    body.getUnits().insertBefore(
                            Jimple.v().newAssignStmt(
                                    Jimple.v().newInstanceFieldRef(
                                            body.getThisLocal(),
                                            indexArrayField.makeRef()),
                                    indexesLocal), insertPoint);
                }

                // If the port is an input, then it references
View Full Code Here

Examples of soot.jimple.JimpleBody

        // 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();
            Stmt insertPoint = (Stmt) body.getUnits().getLast();

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

            //             <body@insertPoint,
            //                     $tokenType[] buffer;  // RefType
            //                     $tokenType[][] channelLocal;
            //                     channel = new $tokenType[$(port.getWidth())]; // int expression
            //                     $bufferField = channelLocal;> // SootField
            //             int channel = 0;
            //             for (Iterator relations = port.linkedRelationList().iterator();
            //                 relations.hasNext();) {
            //                 TypedIORelation relation = (TypedIORelation)relations.next();
            //                 for (int i = 0; i < relation.getWidth(); i++, channel++) {
            //                     String fieldName =
            //                         InlinePortTransformer.getBufferFieldName(
            //                                 relation, i, type);
            //                     Local containerLocal =
            //                         FieldsForEntitiesTransformer.getLocalReferenceForEntity(
            //                                 _model, entityClass, body.getThisLocal(),
            //                                 body, insertPoint, _options);
            //                     <body@insertPoint,
            //                             buffer = $containerLocal.$fieldName; // Local, String
            //                             channelLocal[$channel] = buffer; // int
            //                 }
            //             }
            //             JavaFragmentParser parser = new StatementParser(body, insertPoint);
            //   ANOTHER TRY:
            //             parser.map("$tokenType", tokenType);
            //             parser.map("$width", IntConstant.v(port.getWidth()));
            //             parser.map("$bufferField", bufferField);
            //             parser.parse(
            //                     "$tokenType[] buffer;  // RefType \\
            //                         $tokenType[][] channelLocal; \\
            //                         channel = new $tokenType[$width]; // int expression \\
            //                         $bufferField = channelLocal;");// SootField
            //             int channel = 0;
            //             for (Iterator relations = port.linkedRelationList().iterator();
            //                 relations.hasNext();) {
            //                 TypedIORelation relation = (TypedIORelation)relations.next();
            //                 for (int i = 0; i < relation.getWidth(); i++, channel++) {
            //                     String fieldName =
            //                         InlinePortTransformer.getBufferFieldName(
            //                                 relation, i, type);
            //                     Local containerLocal =
            //                         FieldsForEntitiesTransformer.getLocalReferenceForEntity(
            //                                 _model, entityClass, body.getThisLocal(),
            //                                 body, insertPoint, _options);
            //                     parser.map("$containerLocal", containerLocal);
            //                     parser.map("$fieldName", fieldName);
            //                     parser.map("$channel", IntConstant.v(channel));
            //                     parser.parse(
            //                             "buffer = $containerLocal.$fieldName; // Local, String \\
            //                             channelLocal[$channel] = buffer; // int");
            //                 }
            //             }
            Local bufferLocal = Jimple.v().newLocal("buffer",
                    ArrayType.v(tokenType, 1));
            body.getLocals().add(bufferLocal);

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

            // Create the array of port channels.
            body.getUnits().insertBefore(
                    Jimple.v().newAssignStmt(
                            channelLocal,
                            Jimple.v().newNewArrayExpr(
                                    ArrayType.v(tokenType, 1),
                                    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);

            // For each channel of the port, make the buffer for that
            // channel point to the appropriate buffer of the relation.
            int channel = 0;

            for (Iterator relations = port.linkedRelationList().iterator(); relations
                    .hasNext();) {
                TypedIORelation relation = (TypedIORelation) relations.next();

                for (int i = 0; i < relation.getWidth(); i++, channel++) {
                    // FIXME: buffersize is only one!
                    //  if (bufsize == 1) {
                    //  } else {
                    // Get the buffer associated with the channel.
                    SootField arrayField = _modelClass
                            .getFieldByName(InlinePortTransformer
                                    .getBufferFieldName(relation, i, type));

                    Local containerLocal = FieldsForEntitiesTransformer
                            .getLocalReferenceForEntity(_model, entityClass,
                                    body.getThisLocal(), body, insertPoint,
                                    _options);

                    // Load the buffer array.
                    body.getUnits()
                            .insertBefore(
                                    Jimple.v().newAssignStmt(
                                            bufferLocal,
                                            Jimple.v().newInstanceFieldRef(
                                                    containerLocal,
                                                    arrayField.makeRef())),
                                    insertPoint);

                    // Store to the port array.
                    body.getUnits().insertBefore(
                            Jimple.v().newAssignStmt(
                                    Jimple.v().newArrayRef(channelLocal,
                                            IntConstant.v(channel)),
                                    bufferLocal), insertPoint);
                }
View Full Code Here

Examples of soot.jimple.JimpleBody

                SootMethod runMethod = new SootMethod(runInterface.getName(),
                        runInterface.getParameterTypes(), runInterface
                                .getReturnType(), Modifier.PUBLIC);
                taskClass.addMethod(runMethod);

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

                Chain units = body.getUnits();

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

                Local actorLocal = Jimple.v().newLocal("actor", actorType);
                body.getLocals().add(actorLocal);

                Local modelLocal = Jimple.v().newLocal("model",
                        RefType.v(modelClass));
                body.getLocals().add(modelLocal);

                Local paramLocal = Jimple.v().newLocal("params",
                        RefType.v(giottoParameterClass));
                body.getLocals().add(paramLocal);

                Local portVarLocal = Jimple
                        .v()
                        .newLocal(
                                "portVar",
                                RefType
                                        .v("giotto.functionality.interfaces.PortVariable"));
                body.getLocals().add(portVarLocal);

                Local tokenPortVarLocal = Jimple.v().newLocal("tokenPortVar",
                        RefType.v(giottoTokenPortVariableClass));
                body.getLocals().add(tokenPortVarLocal);

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

                Local bufferLocal = Jimple.v().newLocal("buffer",
                        ArrayType.v(PtolemyUtilities.tokenType, 1));
                body.getLocals().add(bufferLocal);

                SootMethod actorPrefireMethod = SootUtilities
                        .searchForMethodByName(entityClass, "prefire");
                SootMethod actorFireMethod = SootUtilities
                        .searchForMethodByName(entityClass, "fire");
                SootMethod actorPostfireMethod = SootUtilities
                        .searchForMethodByName(entityClass, "postfire");

                Stmt insertPoint = Jimple.v().newNopStmt();
                units.add(insertPoint);

                // Get a reference to the actor.
                units.insertBefore(Jimple.v().newAssignStmt(modelLocal,
                        Jimple.v().newStaticFieldRef(modelField.makeRef())),
                        insertPoint);
                units.insertBefore(Jimple.v().newAssignStmt(
                        actorLocal,
                        Jimple.v().newVirtualInvokeExpr(modelLocal,
                                PtolemyUtilities.getEntityMethod.makeRef(),
                                StringConstant.v(entity.getName()))),
                        insertPoint);

                // Copy the inputs...
                List inputPortList = ((Actor) entity).inputPortList();
                List outputPortList = ((Actor) entity).outputPortList();
                int inputCount = inputPortList.size();
                int outputCount = outputPortList.size();

                // Get the Parameter argument.
                units.insertBefore(Jimple.v().newAssignStmt(paramLocal,
                        body.getParameterLocal(0)), insertPoint);

                for (int i = 0; i < inputCount; i++) {
                    TypedIOPort port = (TypedIOPort) inputPortList.get(i);

                    // Get the port variable from the parameter.
                    units.insertBefore(Jimple.v().newAssignStmt(
                            portVarLocal,
                            Jimple.v().newVirtualInvokeExpr(paramLocal,
                                    getPortVariableMethod.makeRef(),
                                    IntConstant.v(i))), insertPoint);

                    // Cast the port variable to the correct type.
                    units.insertBefore(Jimple.v().newAssignStmt(
                            tokenPortVarLocal,
                            Jimple.v().newCastExpr(portVarLocal,
                                    RefType.v(giottoTokenPortVariableClass))),
                            insertPoint);

                    // Get the token from the port variable.
                    units.insertBefore(Jimple.v().newAssignStmt(
                            tokenLocal,
                            Jimple.v().newVirtualInvokeExpr(tokenPortVarLocal,
                                    getPortVariableTokenMethod.makeRef())),
                            insertPoint);

                    // Get the buffer to put the token into.
                    units.insertBefore(Jimple.v().newAssignStmt(
                            bufferLocal,
                            Jimple.v().newInstanceFieldRef(
                                    actorLocal,
                                    portInliner.getBufferField(port,
                                            port.getType()).makeRef())),
                            insertPoint);

                    // Store the token.
                    units
                            .insertBefore(Jimple.v().newAssignStmt(
                                    Jimple.v().newArrayRef(bufferLocal,
                                            IntConstant.v(0)), tokenLocal),
                                    insertPoint);
                }

                // Create the code to actually fire the actor.
                units.insertBefore(Jimple.v().newInvokeStmt(
                        Jimple.v().newVirtualInvokeExpr(actorLocal,
                                actorPrefireMethod.makeRef())), insertPoint);
                units.insertBefore(Jimple.v().newInvokeStmt(
                        Jimple.v().newVirtualInvokeExpr(actorLocal,
                                actorFireMethod.makeRef())), insertPoint);
                units.insertBefore(Jimple.v().newAssignStmt(
                        localPostfireReturnsLocal,
                        Jimple.v().newVirtualInvokeExpr(actorLocal,
                                actorPostfireMethod.makeRef())), insertPoint);

                // The Parameter.
                units.insertBefore(Jimple.v().newAssignStmt(paramLocal,
                        body.getParameterLocal(0)), insertPoint);

                // Copy the outputs
                // FIXME! loop
                for (int i = 0; i < outputCount; i++) {
                    TypedIOPort port = (TypedIOPort) outputPortList.get(i);

                    // Get the buffer to retrieve the token from.
                    units.insertBefore(Jimple.v().newAssignStmt(
                            bufferLocal,
                            Jimple.v().newInstanceFieldRef(
                                    actorLocal,
                                    portInliner.getBufferField(port,
                                            port.getType()).makeRef())),
                            insertPoint);

                    // Retrieve the token.
                    units.insertBefore(Jimple.v().newAssignStmt(
                            tokenLocal,
                            Jimple.v().newArrayRef(bufferLocal,
                                    IntConstant.v(0))), insertPoint);

                    // Get the right output Port variable.
                    units.insertBefore(Jimple.v().newAssignStmt(
                            portVarLocal,
                            Jimple.v().newVirtualInvokeExpr(paramLocal,
                                    getPortVariableMethod.makeRef(),
                                    IntConstant.v(inputCount + i))),
                            insertPoint);

                    // Cast to a Token port variable.
                    units.insertBefore(Jimple.v().newAssignStmt(
                            tokenPortVarLocal,
                            Jimple.v().newCastExpr(portVarLocal,
                                    RefType.v(giottoTokenPortVariableClass))),
                            insertPoint);

                    // Set the token.
                    units.insertBefore(Jimple.v().newInvokeStmt(
                            Jimple.v().newVirtualInvokeExpr(tokenPortVarLocal,
                                    setPortVariableTokenMethod.makeRef(),
                                    tokenLocal)), insertPoint);
                }

                body.getUnits().add(Jimple.v().newReturnVoidStmt());
            }

            // For each output port in the actor, create an initial
            // value driver.
            List outputPortList = ((Actor) entity).outputPortList();
            int outputCount = outputPortList.size();

            for (int i = 0; i < outputCount; i++) {
                TypedIOPort port = (TypedIOPort) outputPortList.get(i);
                String portID = StringUtilities.sanitizeName(port
                        .getName(model));
                String driverClassName = PhaseOptions.getString(options,
                        "targetPackage")
                        + ".CG" + "init_" + portID;
                SootClass driverInterface = Scene.v().loadClassAndSupport(
                        "giotto.functionality.interfaces.DriverInterface");
                SootMethod driverRunInterface = driverInterface
                        .getMethodByName("run");

                // create a class for the entity instance.
                SootClass driverClass = new SootClass(driverClassName,
                        Modifier.PUBLIC);

                driverClass.setSuperclass(PtolemyUtilities.objectClass);
                driverClass.addInterface(driverInterface);
                driverClass.addInterface(serializationInterface);
                Scene.v().addClass(driverClass);
                driverClass.setApplicationClass();

                // Create a super constructor.
                PtolemyUtilities.createSuperConstructor(driverClass,
                        objectConstructor);

                // Implement the run method.
                SootMethod driverRunMethod = new SootMethod(driverRunInterface
                        .getName(), driverRunInterface.getParameterTypes(),
                        driverRunInterface.getReturnType(), Modifier.PUBLIC);
                driverClass.addMethod(driverRunMethod);

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

                Chain units = body.getUnits();

                Local actorLocal = Jimple.v().newLocal("actor", actorType);
                body.getLocals().add(actorLocal);

                Local modelLocal = Jimple.v().newLocal("model",
                        RefType.v(modelClass));
                body.getLocals().add(modelLocal);

                Local paramLocal = Jimple.v().newLocal("params",
                        RefType.v(giottoParameterClass));
                body.getLocals().add(paramLocal);

                Local portVarLocal = Jimple
                        .v()
                        .newLocal(
                                "portVar",
                                RefType
                                        .v("giotto.functionality.interfaces.PortVariable"));
                body.getLocals().add(portVarLocal);

                Local tokenPortVarLocal = Jimple.v().newLocal("tokenPortVar",
                        RefType.v(giottoTokenPortVariableClass));
                body.getLocals().add(tokenPortVarLocal);

                Stmt insertPoint = Jimple.v().newNopStmt();
                units.add(insertPoint);

                // Get a reference to the actor.
                units.insertBefore(Jimple.v().newAssignStmt(modelLocal,
                        Jimple.v().newStaticFieldRef(modelField.makeRef())),
                        insertPoint);
                units.insertBefore(Jimple.v().newAssignStmt(
                        actorLocal,
                        Jimple.v().newVirtualInvokeExpr(modelLocal,
                                PtolemyUtilities.getEntityMethod.makeRef(),
                                StringConstant.v(entity.getName()))),
                        insertPoint);

                Local initialValueLocal = Jimple.v()
                        .newLocal("initialValueAttribute",
                                PtolemyUtilities.attributeType);
                body.getLocals().add(initialValueLocal);

                Local initialValueVariableLocal = Jimple.v().newLocal(
                        "initialValueVariable",
                        RefType.v(PtolemyUtilities.variableClass));
                body.getLocals().add(initialValueVariableLocal);

                Parameter initialValueParameter = (Parameter) ((NamedObj) port)
                        .getAttribute("initialValue");

                if (initialValueParameter != null) {
                    String initialValueNameInContext = initialValueParameter
                            .getName(entity);

                    body
                            .getUnits()
                            .insertBefore(
                                    Jimple
                                            .v()
                                            .newAssignStmt(
                                                    initialValueLocal,
                                                    Jimple
                                                            .v()
                                                            .newVirtualInvokeExpr(
                                                                    actorLocal,
                                                                    PtolemyUtilities.getAttributeMethod
                                                                            .makeRef(),
                                                                    StringConstant
                                                                            .v(initialValueNameInContext))),
                                    insertPoint);

                    // cast to Variable.
                    body
                            .getUnits()
                            .insertBefore(
                                    Jimple
                                            .v()
                                            .newAssignStmt(
                                                    initialValueVariableLocal,
                                                    Jimple
                                                            .v()
                                                            .newCastExpr(
                                                                    initialValueLocal,
                                                                    RefType
                                                                            .v(PtolemyUtilities.variableClass))),
                                    insertPoint);

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

                    // call getToken.
                    body
                            .getUnits()
                            .insertBefore(
                                    Jimple
                                            .v()
                                            .newAssignStmt(
                                                    tokenLocal,
                                                    Jimple
                                                            .v()
                                                            .newVirtualInvokeExpr(
                                                                    initialValueVariableLocal,
                                                                    PtolemyUtilities.variableGetTokenMethod
                                                                            .makeRef())),
                                    insertPoint);

                    // Get the Parameter argument.
                    units.insertBefore(Jimple.v().newAssignStmt(paramLocal,
                            body.getParameterLocal(0)), insertPoint);

                    // Get the right output Port variable.
                    units.insertBefore(Jimple.v().newAssignStmt(
                            portVarLocal,
                            Jimple.v().newVirtualInvokeExpr(paramLocal,
                                    getPortVariableMethod.makeRef(),
                                    IntConstant.v(0))), insertPoint);

                    // Cast to a Token port variable.
                    units.insertBefore(Jimple.v().newAssignStmt(
                            tokenPortVarLocal,
                            Jimple.v().newCastExpr(portVarLocal,
                                    RefType.v(giottoTokenPortVariableClass))),
                            insertPoint);

                    // Set the token.
                    units.insertBefore(Jimple.v().newInvokeStmt(
                            Jimple.v().newVirtualInvokeExpr(tokenPortVarLocal,
                                    setPortVariableTokenMethod.makeRef(),
                                    tokenLocal)), insertPoint);
                }

                units.add(Jimple.v().newReturnVoidStmt());
            }
        }
        // Inline the director
        {
            // populate the preinitialize method
            SootMethod classMethod = modelClass
                    .getMethodByName("preinitialize");
            JimpleBody body = (JimpleBody) classMethod.getActiveBody();
            Stmt insertPoint = body.getFirstNonIdentityStmt();

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

            // Set the static field pointing to the model.
            units.insertBefore(Jimple.v().newAssignStmt(
                    Jimple.v().newStaticFieldRef(modelField.makeRef()),
                    thisLocal), insertPoint);

            // Add code to the beginning of the preinitialize method that
            // initializes the attributes.
            //             ModelTransformer.initializeAttributesBefore(body, insertPoint,
            //                     model, body.getThisLocal(),
            //                     model, body.getThisLocal(),
            //                     modelClass);
            for (Iterator entities = model.deepEntityList().iterator(); entities
                    .hasNext();) {
                Entity entity = (Entity) entities.next();
                String fieldName = ModelTransformer.getFieldNameForEntity(
                        entity, model);
                SootField field = modelClass.getFieldByName(fieldName);
                String className = ModelTransformer.getInstanceClassName(
                        entity, options);
                SootClass theClass = Scene.v().loadClassAndSupport(className);
                SootMethod preinitializeMethod = SootUtilities
                        .searchForMethodByName(theClass, "preinitialize");
                Local actorLocal = Jimple.v().newLocal("actor",
                        RefType.v(theClass));
                body.getLocals().add(actorLocal);

                // Get the actor.
                units.insertBefore(Jimple.v().newAssignStmt(
                        actorLocal,
                        Jimple.v().newInstanceFieldRef(thisLocal,
                                field.makeRef())), insertPoint);
                units.insertBefore(Jimple.v().newInvokeStmt(
                        Jimple.v().newVirtualInvokeExpr(actorLocal,
                                preinitializeMethod.makeRef())), insertPoint);
            }

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

        {
            // populate the initialize method
            SootMethod classMethod = modelClass.getMethodByName("initialize");
            JimpleBody body = (JimpleBody) classMethod.getActiveBody();
            Stmt insertPoint = body.getFirstNonIdentityStmt();

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

            Local actorLocal = Jimple.v().newLocal("actor", actorType);
            body.getLocals().add(actorLocal);

            for (Iterator entities = model.deepEntityList().iterator(); entities
                    .hasNext();) {
                Entity entity = (Entity) entities.next();
                String fieldName = ModelTransformer.getFieldNameForEntity(
                        entity, model);
                SootField field = modelClass.getFieldByName(fieldName);
                String className = ModelTransformer.getInstanceClassName(
                        entity, options);
                SootClass theClass = Scene.v().loadClassAndSupport(className);
                SootMethod initializeMethod = SootUtilities
                        .searchForMethodByName(theClass, "initialize");

                // Set the field.
                units.insertBefore(Jimple.v().newAssignStmt(
                        actorLocal,
                        Jimple.v().newInstanceFieldRef(thisLocal,
                                field.makeRef())), insertPoint);
                units.insertBefore(Jimple.v().newInvokeStmt(
                        Jimple.v().newVirtualInvokeExpr(actorLocal,
                                initializeMethod.makeRef())), insertPoint);
            }

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

        {
            // populate the prefire method
            SootMethod classMethod = modelClass.getMethodByName("prefire");
            JimpleBody body = (JimpleBody) classMethod.getActiveBody();
            Stmt insertPoint = body.getFirstNonIdentityStmt();

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

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

            //             // Prefire the controller.
            //             Local actorLocal = Jimple.v().newLocal("actor", actorType);
            //             body.getLocals().add(actorLocal);
            //             String fieldName = ModelTransformer.getFieldNameForEntity(
            //                     controller, model);
            //             SootField field = modelClass.getFieldByName(fieldName);
            //             String className =
            //                 ModelTransformer.getInstanceClassName(controller, options);
            //             SootClass theClass = Scene.v().loadClassAndSupport(className);
            //             SootMethod actorPrefireMethod =
            //                 SootUtilities.searchForMethodByName(
            //                         theClass, "prefire");
            //             units.insertBefore(Jimple.v().newAssignStmt(actorLocal,
            //                     Jimple.v().newInstanceFieldRef(thisLocal, field)),
            //                     insertPoint);
            //             units.insertBefore(Jimple.v().newAssignStmt(prefireReturnsLocal,
            //                               Jimple.v().newVirtualInvokeExpr(actorLocal,
            //                                       actorPrefireMethod)),
            //                     insertPoint);
            units.insertBefore(Jimple.v().newAssignStmt(prefireReturnsLocal,
                    IntConstant.v(0)), insertPoint);
            units.insertBefore(Jimple.v().newReturnStmt(prefireReturnsLocal),
                    insertPoint);

            LocalSplitter.v().transform(body, phaseName + ".lns");
            LocalNameStandardizer.v().transform(body, phaseName + ".lns");
            TypeResolver.resolve(body, Scene.v());
        }

        {
            // populate the fire method
            SootMethod classMethod = modelClass.getMethodByName("fire");
            JimpleBody body = (JimpleBody) classMethod.getActiveBody();
            Stmt insertPoint = body.getFirstNonIdentityStmt();

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

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

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

            // Transfer Inputs from input ports.
            for (Iterator ports = model.inputPortList().iterator(); ports
                    .hasNext();) {
                IOPort port = (IOPort) ports.next();
                int rate = 1;

                String fieldName = ModelTransformer.getFieldNameForPort(port,
                        model);
                SootField field = modelClass.getFieldByName(fieldName);

                // Get a reference to the port.
                Local portLocal = Jimple.v().newLocal("port",
                        PtolemyUtilities.ioportType);
                body.getLocals().add(portLocal);

                Local tempPortLocal = Jimple.v().newLocal("tempPort",
                        PtolemyUtilities.ioportType);
                body.getLocals().add(tempPortLocal);
                units.insertBefore(Jimple.v().newAssignStmt(
                        tempPortLocal,
                        Jimple.v().newInstanceFieldRef(thisLocal,
                                field.makeRef())), insertPoint);
                units.insertBefore(Jimple.v().newAssignStmt(
                        portLocal,
                        Jimple.v().newCastExpr(tempPortLocal,
                                PtolemyUtilities.ioportType)), insertPoint);

                for (int i = 0; i < port.getWidth(); i++) {
                    // The list of initializer instructions.
                    List initializerList = new LinkedList();
                    initializerList.add(Jimple.v().newAssignStmt(indexLocal,
                            IntConstant.v(0)));

                    // The list of body instructions.
                    List bodyList = new LinkedList();

                    // Read
                    bodyList.add(Jimple.v().newAssignStmt(
                            tokenLocal,
                            Jimple.v().newVirtualInvokeExpr(portLocal,
                                    PtolemyUtilities.getMethod.makeRef(),
                                    IntConstant.v(i))));

                    // Write
                    bodyList.add(Jimple.v().newInvokeStmt(
                            Jimple.v()
                                    .newVirtualInvokeExpr(
                                            portLocal,
                                            PtolemyUtilities.sendInsideMethod
                                                    .makeRef(),
                                            IntConstant.v(i), tokenLocal)));

                    // Increment the index.
                    bodyList.add(Jimple.v()
                            .newAssignStmt(
                                    indexLocal,
                                    Jimple.v().newAddExpr(indexLocal,
                                            IntConstant.v(1))));

                    Expr conditionalExpr = Jimple.v().newLtExpr(indexLocal,
                            IntConstant.v(rate));

                    SootUtilities.createForLoopBefore(body, insertPoint,
                            initializerList, bodyList, conditionalExpr);
                }
            }
            // Start the Emachine...
            {
                // Fire the controller.
                SootClass emulatorClass = Scene.v().loadClassAndSupport(
                        "platform.emachine.java.Emulator");
                SootMethod theRunMethod = emulatorClass
                        .getMethodByName("runAndWait");
                String fileName = directory + "/out.ecd";
                units.insertBefore(Jimple.v().newInvokeStmt(
                        Jimple.v().newStaticInvokeExpr(theRunMethod.makeRef(),
                                StringConstant.v(fileName))), insertPoint);

                //                 Local actorLocal = Jimple.v().newLocal("actor", actorType);
                //                 body.getLocals().add(actorLocal);
                //                 String fieldName = ModelTransformer.getFieldNameForEntity(
                //                         controller, model);
                //                 SootField field = modelClass.getFieldByName(fieldName);
                //                 String className =
                //                     ModelTransformer.getInstanceClassName(controller, options);
                //                 SootClass theClass = Scene.v().loadClassAndSupport(className);
                //                 SootMethod actorFireMethod =
                //                     SootUtilities.searchForMethodByName(
                //                             theClass, "fire");
                //                 units.insertBefore(
                //                         Jimple.v().newAssignStmt(actorLocal,
                //                                 Jimple.v().newInstanceFieldRef(thisLocal, field)),
                //                         insertPoint);
                //                 units.insertBefore(
                //                         Jimple.v().newInvokeStmt(
                //                                 Jimple.v().newVirtualInvokeExpr(actorLocal,
                //                                         actorFireMethod)),
                //                         insertPoint);
            }

            // Transfer outputs from output ports
            for (Iterator ports = model.outputPortList().iterator(); ports
                    .hasNext();) {
                IOPort port = (IOPort) ports.next();
                int rate = DFUtilities.getTokenProductionRate(port);

                String fieldName = ModelTransformer.getFieldNameForPort(port,
                        model);
                SootField field = modelClass.getFieldByName(fieldName);

                // Get a reference to the port.
                Local portLocal = Jimple.v().newLocal("port",
                        PtolemyUtilities.ioportType);
                body.getLocals().add(portLocal);

                Local tempPortLocal = Jimple.v().newLocal("tempPort",
                        PtolemyUtilities.ioportType);
                body.getLocals().add(tempPortLocal);
                units.insertBefore(Jimple.v().newAssignStmt(
                        tempPortLocal,
                        Jimple.v().newInstanceFieldRef(thisLocal,
                                field.makeRef())), insertPoint);
                units.insertBefore(Jimple.v().newAssignStmt(
                        portLocal,
                        Jimple.v().newCastExpr(tempPortLocal,
                                PtolemyUtilities.ioportType)), insertPoint);

                for (int i = 0; i < port.getWidth(); i++) {
                    // The list of initializer instructions.
                    List initializerList = new LinkedList();
                    initializerList.add(Jimple.v().newAssignStmt(indexLocal,
                            IntConstant.v(0)));

                    // The list of body instructions.
                    List bodyList = new LinkedList();

                    // Read
                    bodyList.add(Jimple.v().newAssignStmt(
                            tokenLocal,
                            Jimple.v().newVirtualInvokeExpr(portLocal,
                                    PtolemyUtilities.getInsideMethod.makeRef(),
                                    IntConstant.v(i))));

                    // Write
                    bodyList.add(Jimple.v().newInvokeStmt(
                            Jimple.v().newVirtualInvokeExpr(portLocal,
                                    PtolemyUtilities.sendMethod.makeRef(),
                                    IntConstant.v(i), tokenLocal)));

                    // Increment the index.
                    bodyList.add(Jimple.v()
                            .newAssignStmt(
                                    indexLocal,
                                    Jimple.v().newAddExpr(indexLocal,
                                            IntConstant.v(1))));

                    Expr conditionalExpr = Jimple.v().newLtExpr(indexLocal,
                            IntConstant.v(rate));

                    SootUtilities.createForLoopBefore(body, insertPoint,
                            initializerList, bodyList, conditionalExpr);
                }
            }

            // Return.
            //            units.add(Jimple.v().newReturnVoidStmt());
            LocalSplitter.v().transform(body, phaseName + ".lns");
            LocalNameStandardizer.v().transform(body, phaseName + ".lns");
            TypeResolver.resolve(body, Scene.v());
        }

        {
            // populate the postfire method
            SootMethod classMethod = modelClass.getMethodByName("postfire");
            JimpleBody body = (JimpleBody) classMethod.getActiveBody();
            Stmt insertPoint = body.getFirstNonIdentityStmt();

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

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

            // Postfire the controller.
            //             Local actorLocal = Jimple.v().newLocal("actor", actorType);
            //             body.getLocals().add(actorLocal);
            //             String fieldName = ModelTransformer.getFieldNameForEntity(
            //                     controller, model);
            //             SootField field = modelClass.getFieldByName(fieldName);
            //             String className =
            //                 ModelTransformer.getInstanceClassName(controller, options);
            //             SootClass theClass = Scene.v().loadClassAndSupport(className);
            //             SootMethod actorPostfireMethod =
            //                 SootUtilities.searchForMethodByName(
            //                         theClass, "postfire");
            //             units.insertBefore(Jimple.v().newAssignStmt(actorLocal,
            //                     Jimple.v().newInstanceFieldRef(thisLocal, field)),
            //                     insertPoint);
            //             units.insertBefore(Jimple.v().newAssignStmt(postfireReturnsLocal,
            //                               Jimple.v().newVirtualInvokeExpr(actorLocal,
            //                                       actorPostfireMethod.makeRef())),
            //                     insertPoint);
            units.insertBefore(Jimple.v().newAssignStmt(postfireReturnsLocal,
                    IntConstant.v(0)), insertPoint);

            units.insertBefore(Jimple.v().newReturnStmt(postfireReturnsLocal),
                    insertPoint);
            LocalSplitter.v().transform(body, phaseName + ".lns");
            LocalNameStandardizer.v().transform(body, phaseName + ".lns");
            TypeResolver.resolve(body, Scene.v());
        }

        {
            // populate the wrapup method
            SootMethod classMethod = modelClass.getMethodByName("wrapup");
            JimpleBody body = (JimpleBody) classMethod.getActiveBody();
            Stmt insertPoint = body.getFirstNonIdentityStmt();

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

            Local actorLocal = Jimple.v().newLocal("actor", actorType);
            body.getLocals().add(actorLocal);

            for (Iterator entities = model.deepEntityList().iterator(); entities
                    .hasNext();) {
                Entity entity = (Entity) entities.next();
                String fieldName = ModelTransformer.getFieldNameForEntity(
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.