Package soot.util

Examples of soot.util.Chain


      units.insertBefore(first,units.getFirst());
    } else {
      first = (Unit)units.getSuccOf(first);
    }
   
    Chain locs = b.getLocals();
    HashMap stackHeightsBefore = null;
    HashMap bafToJLocals = soot.jbco.Main.methods2Baf2JLocals.get(b.getMethod());
    int varCount = trapCount + 1;
    traps = b.getTraps().snapshotIterator();
    while (traps.hasNext()) {
      Trap t = (Trap) traps.next();
      Unit begUnit = t.getBeginUnit();
      if (!isRewritable(t) || Rand.getInt(10) > weight)
        continue;
     
      stackHeightsBefore = StackTypeHeightCalculator.calculateStackHeights(b,bafToJLocals);
      boolean badType = false;
      Stack s = (Stack)((Stack)stackHeightsBefore.get(begUnit)).clone();
      if (s.size() > 0) {
        for (int i = 0; i < s.size(); i++) {
          if (s.pop() instanceof StmtAddressType) {
            badType = true;
            break;
          }
        }
      }
      if (badType) continue;
     
      // local to hold control flow flag (0=try, 1=catch)
      Local controlLocal = Baf.v().newLocal("controlLocal_tccomb" + trapCount,
          IntType.v());
      locs.add(controlLocal);

      // initialize local to 0=try
      Unit pushZero = Baf.v().newPushInst(IntConstant.v(0));
      Unit storZero = Baf.v().newStoreInst(IntType.v(), controlLocal);
     
      // this is necessary even though it seems like it shouldn't be
      units.insertBeforeNoRedirect((Unit)pushZero.clone(), first);
      units.insertBeforeNoRedirect((Unit)storZero.clone(), first);
     
      BriefUnitGraph graph = new BriefUnitGraph(b);
      List l = graph.getPredsOf(begUnit);

      // add initializer seq for try - sets local to zero and loads null exc
      units.add(pushZero);
      units.add(storZero);
     
      Stack varsToLoad = new Stack();
      s = (Stack)stackHeightsBefore.get(begUnit);
      if (s.size() > 0) {
        for (int i = 0; i < s.size(); i++) {
          Type type = (Type)s.pop();         
         
          Local varLocal = Baf.v().newLocal("varLocal_tccomb" + varCount++, type);
          locs.add(varLocal);
          varsToLoad.push(varLocal);
          units.add(Baf.v().newStoreInst(type,varLocal));
         
          units.insertBeforeNoRedirect(FixUndefinedLocals.getPushInitializer(varLocal, type), first);
          units.insertBeforeNoRedirect(Baf.v().newStoreInst(type, varLocal), first);
View Full Code Here


              ExceptionalUnitGraph eug = new ExceptionalUnitGraph(b);
              methodToExcUnitGraph.put(method, eug);
             
              // run the intraprocedural analysis
            SynchronizedRegionFinder ta = new SynchronizedRegionFinder(eug, b, optionPrintDebug, optionOpenNesting, tlo);
            Chain units = b.getUnits();
            Unit lastUnit = (Unit) units.getLast();
            FlowSet fs = (FlowSet) ta.getFlowBefore(lastUnit);
         
            // add the results to the list of results
            methodToFlowSet.put(method, fs);
        }
View Full Code Here

                for (Iterator methods = entityClass.getMethods().iterator(); (methods
                        .hasNext() && finalize);) {
                    SootMethod method = (SootMethod) methods.next();

                    if (method.getName().equals("<init>")) {
                        Chain units = method.retrieveActiveBody().getUnits();
                        Stmt stmt = (Stmt) units.getLast();

                        while (!stmt.equals(units.getFirst())) {
                            if (stmt instanceof DefinitionStmt
                                    && ((DefinitionStmt) stmt).getLeftOp() instanceof InstanceFieldRef) {
                                InstanceFieldRef ref = (InstanceFieldRef) ((DefinitionStmt) stmt)
                                        .getLeftOp();

                                if ((ref.getField() == field)
                                        && (fieldValue == null)) {
                                    fieldValue = ((DefinitionStmt) stmt)
                                            .getRightOp();
                                    break;
                                } else if (fieldValue != null) {
                                    finalize = false;
                                }
                            }

                            stmt = (Stmt) units.getPredOf(stmt);
                        }
                    }
                }

                if (finalize && (fieldValue != null)) {
View Full Code Here

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

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

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

            // Create and initialize ports
            ModelTransformer.createPorts(body, thisLocal, entity, thisLocal,
                    entity, entityInstanceClass, tempCreatedMap);

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

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

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

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

                SootField field = new SootField(StringUtilities
                        .sanitizeName(name)
                        + "Token", type);
                entityInstanceClass.addField(field);
                nameToField.put(name, field);
            }
        }
        // Populate the fire method.
        {
            SootMethod fireMethod = new SootMethod("fire",
                    Collections.EMPTY_LIST, VoidType.v(), Modifier.PUBLIC);
            entityInstanceClass.addMethod(fireMethod);

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

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

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

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

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

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

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

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

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

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

                    SootField tokenField = entityInstanceClass
                            .getFieldByName(name + "Token");
                    units.add(Jimple.v().newAssignStmt(
                            Jimple.v().newInstanceFieldRef(thisLocal,
                                    tokenField.makeRef()), tokenLocal));
                    units.add(target);
                }
            }

            StringAttribute expressionAttribute = (StringAttribute) entity
                    .getAttribute("expression");
            String expression = expressionAttribute.getExpression();

            Local local = DataUtilities.generateExpressionCode(entity,
                    entityInstanceClass, expression, nameToField, nameToType,
                    body);

            // send the computed token
            String name = "output";
            Local portLocal = Jimple.v().newLocal("port",
                    PtolemyUtilities.componentPortType);
            body.getLocals().add(portLocal);

            SootField portField = entityInstanceClass.getFieldByName(name);

            units.add(Jimple.v().newAssignStmt(
                    portLocal,
                    Jimple.v().newInstanceFieldRef(thisLocal,
                            portField.makeRef())));
            units.add(Jimple.v().newInvokeStmt(
                    Jimple.v().newVirtualInvokeExpr(portLocal,
                            PtolemyUtilities.sendMethod.makeRef(),
                            IntConstant.v(0), local)));

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

            LocalNameStandardizer.v().transform(body, "at.lns");
            LocalSplitter.v().transform(body, "at.ls");
        }
View Full Code Here

                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(
                        actorLocal,
                        Jimple.v().newInstanceFieldRef(thisLocal,
                                field.makeRef())), insertPoint);
                units.insertBefore(Jimple.v().newInvokeStmt(
                        Jimple.v().newVirtualInvokeExpr(actorLocal,
                                wrapupMethod.makeRef())), insertPoint);
            }

            //           units.insertBefore(Jimple.v().newReturnVoidStmt(),
View Full Code Here

            }

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

            // Call the __CGInit method.
            units.add(Jimple.v().newInvokeStmt(
                    Jimple.v().newVirtualInvokeExpr(thisLocal,
                            _initMethod.makeRef())));

            // return void
            units.add(Jimple.v().newReturnVoidStmt());
        }
    }
View Full Code Here

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

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

        // get a list of the locals that reference the parameters of the
        // constructor.  What a nice hack.
        List parameterList = new ArrayList();
        parameterList.addAll(body.getLocals());
        parameterList.remove(thisLocal);

        // Call the super constructor.
        units.add(Jimple.v().newInvokeStmt(
                Jimple.v().newSpecialInvokeExpr(thisLocal,
                        superConstructor.makeRef(), parameterList)));

        return constructor;
    }
View Full Code Here

            clinitBody = Jimple.v().newBody(clinitMethod);
            clinitMethod.setActiveBody(clinitBody);
            clinitBody.getUnits().add(Jimple.v().newReturnVoidStmt());
        }

        Chain clinitUnits = clinitBody.getUnits();

        // Loop over all the relations, creating buffers for each channel.
        for (Iterator relations = _model.relationList().iterator(); relations
                .hasNext();) {
            TypedIORelation relation = (TypedIORelation) relations.next();

            // Determine the types that the relation is connected to.
            Map typeMap = new HashMap();
            List destinationPortList = relation.linkedDestinationPortList();

            for (Iterator destinationPorts = destinationPortList.iterator(); destinationPorts
                    .hasNext();) {
                TypedIOPort port = (TypedIOPort) destinationPorts.next();
                ptolemy.data.type.Type type = port.getType();
                typeMap.put(type.toString(), type);
            }

            for (Iterator types = typeMap.keySet().iterator(); types.hasNext();) {
                ptolemy.data.type.Type type = (ptolemy.data.type.Type) typeMap
                        .get(types.next());
                RefType tokenType = PtolemyUtilities
                        .getSootTypeForTokenType(type);

                String fieldName = relation.getName() + "_bufferLocal";
                Local arrayLocal = Jimple.v().newLocal(fieldName, tokenType);
                clinitBody.getLocals().add(arrayLocal);

                for (int i = 0; i < relation.getWidth(); i++) {
                    SootField field = new SootField(InlinePortTransformer
                            .getBufferFieldName(relation, i, type), tokenType,
                            Modifier.PUBLIC | Modifier.STATIC);
                    _modelClass.addField(field);

                    // Tag the field with the type.
                    field.addTag(new TypeTag(type));

                    // Note: reverse order!
                    clinitUnits.addFirst(Jimple.v().newAssignStmt(
                            Jimple.v().newStaticFieldRef(field.makeRef()),
                            NullConstant.v()));
                }
            }
        }
View Full Code Here

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

     *  the given local.
     */
    public static SootField createAndSetFieldFromLocal(JimpleBody body,
            Local local, SootClass theClass, Type type, String name,
            Unit insertPoint) {
        Chain units = body.getUnits();
        Local thisLocal = body.getThisLocal();

        Local castLocal;

        if (local.getType().equals(type)) {
            castLocal = local;
        } else {
            castLocal = Jimple.v().newLocal("local_" + name, type);
            body.getLocals().add(castLocal);

            // Cast the local to the type of the field.
            units.insertAfter(Jimple.v().newAssignStmt(castLocal,
                    Jimple.v().newCastExpr(local, type)), insertPoint);
            insertPoint = (Unit) body.getUnits().getSuccOf(insertPoint);
        }

        // Create the new field if necessary
        SootField field;

        if (theClass.declaresFieldByName(name)) {
            field = theClass.getFieldByName(name);
        } else {
            field = new SootField(name, type, Modifier.PUBLIC);
            theClass.addField(field);
        }

        // Set the field.
        units.insertAfter(Jimple.v().newAssignStmt(
                Jimple.v().newInstanceFieldRef(thisLocal, field.makeRef()),
                castLocal), insertPoint);
        return field;
    }
View Full Code Here

TOP

Related Classes of soot.util.Chain

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.