Package soot

Examples of soot.SootClass


                // attribute statically evaluates to null.
                return null;
            } else if (value instanceof ThisRef) {
                // Resolve the thisRef for automatically generated
                // attributes.
                SootClass refClass = method.getDeclaringClass();
                return ModelTransformer.getAttributeForClass(refClass);
            } else {
                throw new RuntimeException("Unknown type of value: " + value
                        + " in " + method);
            }
View Full Code Here


        if (debug) {
            System.out.println("creating field for " + container + " in class "
                    + theClass);
        }

        SootClass stringClass = Scene.v().loadClassAndSupport(
                "java.lang.String");
        Type stringType = RefType.v(stringClass);

        for (Iterator attributes = container.attributeList().iterator(); attributes
                .hasNext();) {
            Attribute attribute = (Attribute) attributes.next();

            if (attributeToValueFieldMap.get(attribute) != null) {
                throw new RuntimeException(
                        "already created field for attribute" + attribute);
            }

            if (attribute instanceof Settable) {
                Settable settable = (Settable) attribute;

                if (debug) {
                    System.out.println("creating field for " + settable);
                }

                String fieldName = StringUtilities.sanitizeName(attribute
                        .getName(context));
                SootField field;

                // Create a field to contain the value of the attribute.
                if (settable instanceof Variable) {
                    Variable variable = (Variable) settable;
                    ptolemy.data.type.Type type = variable.getType();
                    Type tokenType = PtolemyUtilities
                            .getSootTypeForTokenType(type);

                    boolean isConstant = constantAnalysis.getConstVariables(
                            context).contains(attribute);

                    int modifier;

                    if (isConstant) {
                        modifier = Modifier.PUBLIC | Modifier.FINAL;
                    } else {
                        modifier = Modifier.PUBLIC;
                    }

                    field = new SootField(fieldName + "_CGToken", tokenType,
                            modifier);
                    theClass.addField(field);

                    if (isConstant) {
                        try {
                            field.addTag(new ValueTag(variable.getToken()));
                        } catch (Exception ex) {
                        }

                        field.addTag(new TypeTag(type));
                    }
                } else {
                    field = new SootField(fieldName + "_CGExpression",
                            stringType, Modifier.PUBLIC | Modifier.FINAL);
                    theClass.addField(field);

                    String expression = settable.getExpression();
                    field.addTag(new ValueTag(expression));
                }

                attributeToValueFieldMap.put(attribute, field);
            }

            _createTokenAndExpressionFields(theClass, context, attribute,
                    attributeToValueFieldMap, constantAnalysis, debug);
        }

        if (container instanceof ComponentEntity) {
            ComponentEntity entity = (ComponentEntity) container;

            for (Iterator ports = entity.portList().iterator(); ports.hasNext();) {
                Port port = (Port) ports.next();
                _createTokenAndExpressionFields(theClass, context, port,
                        attributeToValueFieldMap, constantAnalysis, debug);
            }
        }

        if (container instanceof CompositeEntity
                && !(container instanceof FSMActor)) {
            CompositeEntity model = (CompositeEntity) container;

            // Loop over all the actor 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);

                _createTokenAndExpressionFields(entityClass, entity, entity,
                        attributeToValueFieldMap, constantAnalysis, debug);
            }
View Full Code Here

            }
        }

        for (Iterator classes = Scene.v().getApplicationClasses().iterator(); classes
                .hasNext();) {
            SootClass theClass = (SootClass) classes.next();

            try {
                _write(theClass, outDir);
            } catch (Exception ex) {
                // If we get an IOException, we might not have any idea
View Full Code Here

        // having more than one instance of this generated code!
        SootField modelField = new SootField("_GiottoModel", RefType
                .v(modelClass), Modifier.PUBLIC | Modifier.STATIC);
        modelClass.addField(modelField);

        SootClass giottoParameterClass = Scene.v().loadClassAndSupport(
                "giotto.functionality.table.Parameter");
        SootClass giottoTokenPortVariableClass = Scene.v().loadClassAndSupport(
                "giotto.functionality.code.Token_port");
        SootMethod getPortVariableMethod = SootUtilities.searchForMethodByName(
                giottoParameterClass, "getPortVariable");
        SootMethod getPortVariableTokenMethod = SootUtilities
                .searchForMethodByName(giottoTokenPortVariableClass, "getToken");
        SootMethod setPortVariableTokenMethod = SootUtilities
                .searchForMethodByName(giottoTokenPortVariableClass, "setToken");
        SootMethod objectConstructor = SootUtilities.searchForMethodByName(
                PtolemyUtilities.objectClass, "<init>");
        SootClass serializationInterface = Scene.v().loadClassAndSupport(
                "java.io.Serializable");

        // Create a Task class for each actor in the model
        for (Iterator entities = model.deepEntityList().iterator(); entities
                .hasNext();) {
            Entity entity = (Entity) entities.next();
            String entityClassName = ModelTransformer.getInstanceClassName(
                    entity, options);
            SootClass entityClass = Scene.v().loadClassAndSupport(
                    entityClassName);
            //String fieldName = ModelTransformer.getFieldNameForEntity(entity,
            //        model);
            //SootField field = modelClass.getFieldByName(fieldName);

            String taskClassName = entityClassName + "_Task";
            SootClass taskInterface = Scene.v().loadClassAndSupport(
                    "giotto.functionality.interfaces.TaskInterface");
            SootMethod runInterface = taskInterface.getMethodByName("run");

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

            taskClass.setSuperclass(PtolemyUtilities.objectClass);
            taskClass.addInterface(taskInterface);
            taskClass.addInterface(serializationInterface);
            Scene.v().addClass(taskClass);
            taskClass.setApplicationClass();

            // Create a super constructor.
            PtolemyUtilities.createSuperConstructor(taskClass,
                    objectConstructor);
            // Implement the run method.
            {
                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(
                        entity, model);
                SootField field = modelClass.getFieldByName(fieldName);
                String className = ModelTransformer.getInstanceClassName(
                        entity, options);
                SootClass theClass = Scene.v().loadClassAndSupport(className);
                SootMethod wrapupMethod = SootUtilities.searchForMethodByName(
                        theClass, "wrapup");

                // Set the field.
                units.insertBefore(Jimple.v().newAssignStmt(
View Full Code Here

        forcedReachableMethodSet.addAll(EntryPoints.v().application());

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

            // If we are in actor mode, then assert that all the
            // methods of the toplevel class are reachable.  We need a
            // way of preserving the container, name constructor
            // instead of the no arg constructor for the toplevel.
            //            SootClass modelClass = ModelTransformer.getModelClass();
            //           if (theClass.equals(modelClass)) {
            //                 Set methodSet = _getMethodSet(theClass);
            //                 forcedReachableMethodSet.addAll(methodSet);
            //             }
            // Assume that any method that is part of an interface that this
            // object implements, is reachable.
            //  System.out.println("forcing interfaces of " + theClass);
            if (!theClass.isInterface()) {
                for (Iterator interfaces = theClass.getInterfaces().iterator(); interfaces
                        .hasNext();) {
                    SootClass theInterface = (SootClass) interfaces.next();

                    _addMethodsFrom(forcedReachableMethodSet, theInterface,
                            theClass);
                }
            }
        }

        System.out.println("forcedMethods = " + forcedReachableMethodSet);

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

        System.out.println("done loading classes!");

        // Construct the graph of methods that are directly reachable
        // from any method.
        // Construct the graph of all method invocations, so we know what
        // method contains each invocation and what method(s) can be
        // targeted by that invocation.
        CallGraphBuilder cg = new CallGraphBuilder(DumbPointerAnalysis.v(),
                true);
        cg.build();
        CallGraph callGraph = Scene.v().getCallGraph();
        Scene.v().setCallGraph(callGraph);

        SideEffectAnalysis analysis = new SideEffectAnalysis();

        for (Iterator classes = Scene.v().getApplicationClasses().iterator(); classes
                .hasNext();) {
            SootClass theClass = (SootClass) classes.next();

            for (Iterator methods = theClass.getMethods().iterator(); methods
                    .hasNext();) {
                SootMethod method = (SootMethod) methods.next();
                _removeSideEffectFreeMethodCalls(method, callGraph, analysis);
            }
        }
View Full Code Here

            if (_isIgnorableAttribute(attribute)) {
                continue;
            }

            SootClass attributeClass = (SootClass) _objectToClassMap
                    .get(attribute);

            if (attributeClass == null) {
                attributeClass = Scene.v().loadClassAndSupport(
                        attribute.getClass().getName());
            }

            String className = attributeClass.getName();
            Type attributeType = RefType.v(className);
            String attributeName = attribute.getName(context);
            String fieldName = getFieldNameForAttribute(attribute, context);

            Local local;
View Full Code Here

                {
                    Type type = targetMethod.getReturnType();

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

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

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

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

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

                GenericAtomicActorCreator creator = new GenericAtomicActorCreator();
                creator.createAtomicActor((AtomicActor) entity, newClassName,
                        constAnalysis, options);
            }

            SootClass entityClass = Scene.v().loadClassAndSupport(newClassName);
            addActorForClass(entityClass, entity);
        }
    }
View Full Code Here

    // Populate the given class with code to create the contents of
    // the given entity.
    private static EntitySootClass _createCompositeActor(CompositeActor entity,
            String newClassName, Map options) {
        SootClass entityClass = PtolemyUtilities.compositeActorClass;

        // create a class for the entity instance.
        EntitySootClass entityInstanceClass = new EntitySootClass(entityClass,
                newClassName, Modifier.PUBLIC);
        Scene.v().addClass(entityInstanceClass);
        entityInstanceClass.setApplicationClass();

        // Record everything that the class creates.
        HashMap tempCreatedMap = new HashMap();

        // Create methods that will compute and set the values of the
        // parameters of this actor.
        createAttributeComputationFunctions(entity, entity,
                entityInstanceClass, _constAnalysis);

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

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

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

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

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

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

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

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

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

        implementExecutableInterface(entityInstanceClass);

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

        {
            Set locallyModifiedAttributeSet = _constAnalysis
                    .getVariablesWithChangeContext(entity);

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

            // Sort according to dependencies.
            List locallyModifiedAttributeList;

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

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

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

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

            // Invoke the method to compute each attribute, in order.
            for (Iterator attributes = locallyModifiedAttributeList.iterator(); attributes
                    .hasNext();) {
                Attribute attribute = (Attribute) attributes.next();
                Entity attributeContainer = FieldsForEntitiesTransformer
                        .getEntityContainerOfObject(attribute);
                SootClass containerClass = (SootClass) _objectToClassMap
                        .get(attributeContainer);
                SootMethod computeMethod = containerClass
                        .getMethodByName(getAttributeComputationFunctionName(
                                attribute, attributeContainer));
                body.getUnits().insertBefore(
                        Jimple.v().newAssignStmt(
                                containerLocal,
View Full Code Here

        // Generate all source files for user classes.
        Iterator i = RequiredFileGenerator.getRequiredClasses().iterator();

        while (i.hasNext()) {
            SootClass nextClass = (SootClass) i.next();

            String name = MakeFileGenerator.classNameToMakeFileName(nextClass
                    .getName());

            // Go over each name. If it is not a system class, add it to
            // "sources" else add it to libSources.
            if (!CNames.isSystemClass(nextClass.getName())) {
                code.append("\t" + name + ".c\\\n");
            } else {
                libSources.add(name);
            }
        }
View Full Code Here

TOP

Related Classes of soot.SootClass

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.