Examples of Chain


Examples of soot.util.Chain

            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.
View Full Code Here

Examples of soot.util.Chain

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

        constructorMethod.retrieveActiveBody();

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

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

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

Examples of soot.util.Chain

            newBody.insertIdentityStmts();

            //System.out.println("method = " + newMethod);
            //System.out.println("oldMethod = " + oldMethod);
            // Call the super method
            Chain units = newBody.getUnits();

            // get a list of the locals that reference
            // the parameters of the
            // constructor.
            List parameterList = new ArrayList();
            parameterList.addAll(newBody.getLocals());

            Stmt invokeStmt = null;

            // Invoke the method...
            // handling static and void methods differently
            if (oldMethod.getReturnType() == VoidType.v()) {
                InvokeExpr invokeExpr;

                if (newMethod.isStatic()) {
                    invokeExpr = Jimple.v().newStaticInvokeExpr(
                            oldMethod.makeRef(), parameterList);
                } else {
                    Local thisLocal = newBody.getThisLocal();
                    parameterList.remove(thisLocal);
                    invokeExpr = Jimple.v().newVirtualInvokeExpr(thisLocal,
                            oldMethod.makeRef(), parameterList);
                }

                invokeStmt = Jimple.v().newInvokeStmt(invokeExpr);
                units.add(invokeStmt);

                // return void
                units.add(Jimple.v().newReturnVoidStmt());
            } else {
                InvokeExpr invokeExpr;

                // Create a new local for the return value.
                Local returnValueLocal = Jimple.v().newLocal("returnValue",
                        oldMethod.getReturnType());
                newBody.getLocals().add(returnValueLocal);

                if (newMethod.isStatic()) {
                    invokeExpr = Jimple.v().newStaticInvokeExpr(
                            oldMethod.makeRef(), parameterList);
                } else {
                    Local thisLocal = newBody.getThisLocal();
                    parameterList.remove(thisLocal);
                    invokeExpr = Jimple.v().newVirtualInvokeExpr(thisLocal,
                            oldMethod.makeRef(), parameterList);
                }

                invokeStmt = Jimple.v().newAssignStmt(returnValueLocal,
                        invokeExpr);
                units.add(invokeStmt);

                // return the value
                units.add(Jimple.v().newReturnStmt(returnValueLocal));
            }
        }

        // Loop through all the methods again, this time looking for
        // method invocations on the old superClass...  Inline these calls.
View Full Code Here

Examples of soot.util.Chain

                // There should be a jump from the predecessor to the
                // condition.  Redirect this jump to the body.
                whileCond.getHead().redirectJumpsToThisTo(block.getHead());

                Local thisLocal = body.getThisLocal();
                Chain units = body.getUnits();
                List blockStmtList = new LinkedList();

                // pull the statements that we are inlining out of the block
                // so that we can copy them.  Note that this also removes
                // them from the method body.
                Unit insertPoint = (Unit) units.getSuccOf(block.getTail());

                for (Iterator blockStmts = block.iterator(); blockStmts
                        .hasNext();) {
                    Stmt original = (Stmt) blockStmts.next();
                    blockStmtList.add(original);
                    blockStmts.remove();
                }

                // Remove the jump that should be the final statement.
                blockStmtList.remove(blockStmtList
                        .get(blockStmtList.size() - 1));

                // Loop through and unroll the loop body once for
                // every element of the field list.
                for (Iterator fields = fieldList.iterator(); fields.hasNext();) {
                    SootField insertField = (SootField) fields.next();

                    for (Iterator blockStmts = blockStmtList.iterator(); blockStmts
                            .hasNext();) {
                        // clone each statement
                        Stmt original = (Stmt) blockStmts.next();
                        Stmt clone = (Stmt) original.clone();

                        // If the statement is a call to the next() method,
                        // then inline it with the next value of the iterator.
                        for (Iterator boxes = clone.getUseBoxes().iterator(); boxes
                                .hasNext();) {
                            ValueBox box = (ValueBox) boxes.next();
                            Value value = box.getValue();

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

                                if (r.getMethod() == iteratorNextMethod) {
                                    box.setValue(Jimple.v()
                                            .newInstanceFieldRef(thisLocal,
                                                    insertField.makeRef()));
                                }
                            }
                        }

                        units.insertBefore(clone, insertPoint);
                    }
                }

                // remove the conditional
                for (Iterator blockStmts = whileCond.iterator(); blockStmts
View Full Code Here

Examples of soot.util.Chain

            SootMethod classMethod = modelClass
                    .getMethodByName("preinitialize");
            JimpleBody body = (JimpleBody) classMethod.getActiveBody();
            Stmt insertPoint = body.getFirstNonIdentityStmt();

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

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

            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.makeRef())), insertPoint);
            units.insertBefore(Jimple.v().newAssignStmt(
                    prefireReturnsLocal,
                    Jimple.v().newVirtualInvokeExpr(actorLocal,
                            actorPrefireMethod.makeRef())), 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);
                }
            }

            {
                // Fire 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 actorFireMethod = SootUtilities
                        .searchForMethodByName(theClass, "fire");

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

Examples of soot.util.Chain

     *  the same value as the given token.
     *  @return The new local.
     */
    public static Local buildConstantTokenLocal(JimpleBody body,
            Unit insertPoint, Token token, String localName) {
        Chain units = body.getUnits();

        if (token instanceof ptolemy.data.ArrayToken) {
            ArrayToken arrayToken = (ArrayToken) token;
            RefType tokenType = getSootTypeForTokenType(arrayToken
                    .getElementType());
            Type tokenArrayType = ArrayType.v(tokenType, 1);
            Local tokenArrayLocal = Jimple.v().newLocal(localName + "Array",
                    tokenArrayType);
            body.getLocals().add(tokenArrayLocal);
            Local elementTypeLocal = buildConstantTypeLocal(body, insertPoint,
                    arrayToken.getElementType());

            // Create the array of tokens.
            units.insertBefore(Jimple.v().newAssignStmt(
                    tokenArrayLocal,
                    Jimple.v().newNewArrayExpr(tokenType,
                            IntConstant.v(arrayToken.length()))), insertPoint);

            // recurse
            for (int i = 0; i < arrayToken.length(); i++) {
                Local argLocal = buildConstantTokenLocal(body, insertPoint,
                        arrayToken.getElement(i), localName + "_" + i);
                units.insertBefore(Jimple.v().newAssignStmt(
                        Jimple.v().newArrayRef(tokenArrayLocal,
                                IntConstant.v(i)), argLocal), insertPoint);
            }

            Local tokenLocal = Jimple.v().newLocal(localName,
                    RefType.v(arrayTokenClass));
            body.getLocals().add(tokenLocal);
            units.insertBefore(Jimple.v().newAssignStmt(tokenLocal,
                    Jimple.v().newNewExpr(RefType.v(arrayTokenClass))),
                    insertPoint);
            units.insertBefore(Jimple.v().newInvokeStmt(
                    Jimple.v().newSpecialInvokeExpr(tokenLocal,
                            arrayTokenWithTypeConstructor.makeRef(),
                            elementTypeLocal, tokenArrayLocal)), insertPoint);
            return tokenLocal;
        } else if (token instanceof ptolemy.data.RecordToken) {
            throw new RuntimeException(
                    "Code Generation for RecordTokens is not supported.");

            //             RecordToken recordToken = (RecordToken)token;
            //             int size = recordToken.labelSet().size();
            //             Type stringArrayType =
            //                 ArrayType.v(RefType.v(PtolemyUtilities.stringClass), 1);
            //             Local stringArrayLocal = Jimple.v().newLocal(localName + "SArray",
            //                     stringArrayType);
            //             body.getLocals().add(stringArrayLocal);
            //             // Create the array of strings.
            //             units.insertBefore(
            //                     Jimple.v().newAssignStmt(stringArrayLocal,
            //                             Jimple.v().newNewArrayExpr(
            //                                     RefType.v(PtolemyUtilities.stringClass),
            //                                     IntConstant.v(size))),
            //                     insertPoint);
            //             Type tokenArrayType = ArrayType.v(tokenType, 1);
            //             Local tokenArrayLocal = Jimple.v().newLocal(localName + "TArray",
            //                     tokenArrayType);
            //             body.getLocals().add(tokenArrayLocal);
            //             // Create the array of tokens.
            //             units.insertBefore(Jimple.v().newAssignStmt(tokenArrayLocal,
            //                     Jimple.v().newNewArrayExpr(tokenType,
            //                             IntConstant.v(size))),
            //                     insertPoint);
            //             // recurse
            //             int i = 0;
            //             for (Iterator labels = recordToken.labelSet().iterator();
            //                  labels.hasNext(); i++) {
            //                 String label = (String)labels.next();
            //                 Local argLocal = buildConstantTokenLocal(body, insertPoint,
            //                         recordToken.get(label), localName + "_" + label);
            //                 units.insertBefore(
            //                         Jimple.v().newAssignStmt(
            //                                 Jimple.v().newArrayRef(stringArrayLocal,
            //                                         IntConstant.v(i)),
            //                                 StringConstant.v(label)),
            //                         insertPoint);
            //                 units.insertBefore(
            //                         Jimple.v().newAssignStmt(
            //                                 Jimple.v().newArrayRef(tokenArrayLocal,
            //                                         IntConstant.v(i)),
            //                                 argLocal),
            //                         insertPoint);
            //             }
            //             Local tokenLocal = Jimple.v().newLocal(localName,
            //                     RefType.v(recordTokenClass));
            //             body.getLocals().add(tokenLocal);
            //             units.insertBefore(Jimple.v().newAssignStmt(tokenLocal,
            //                     Jimple.v().newNewExpr(RefType.v(recordTokenClass))),
            //                     insertPoint);
            //             units.insertBefore(Jimple.v().newInvokeStmt(
            //                     Jimple.v().newSpecialInvokeExpr(tokenLocal,
            //                             recordTokenConstructor.makeRef(), stringArrayLocal,
            //                             tokenArrayLocal)),
            //                     insertPoint);
            //             return tokenLocal;
        } else if (token.getClass().equals(Token.class)) {
            // Token has no string constructor.
            SootClass tokenClass = Scene.v().loadClassAndSupport(
                    token.getClass().getName());
            SootMethod tokenConstructor = tokenClass.getMethod("void <init>()");

            Local tokenLocal = Jimple.v().newLocal(localName,
                    RefType.v(tokenClass));
            body.getLocals().add(tokenLocal);
            units.insertBefore(Jimple.v().newAssignStmt(tokenLocal,
                    Jimple.v().newNewExpr(RefType.v(tokenClass))), insertPoint);
            units.insertBefore(Jimple.v().newInvokeStmt(
                    Jimple.v().newSpecialInvokeExpr(tokenLocal,
                            tokenConstructor.makeRef())), insertPoint);
            return tokenLocal;
        } else if (token instanceof IntToken) {
            Local tokenLocal = _buildConstantTokenLocal(body, insertPoint,
                    localName, intTokenClass, intTokenConstructor, IntConstant
                            .v(((IntToken) token).intValue()));
            return tokenLocal;
        } else if (token instanceof UnsignedByteToken) {
            Local tokenLocal = _buildConstantTokenLocal(body, insertPoint,
                    localName, unsignedByteTokenClass,
                    unsignedByteTokenConstructor, IntConstant
                            .v(((UnsignedByteToken) token).byteValue()));
            return tokenLocal;
        } else if (token instanceof BooleanToken) {
            Value value;

            if (((BooleanToken) token).booleanValue()) {
                value = IntConstant.v(1);
            } else {
                value = IntConstant.v(0);
            }

            Local tokenLocal = _buildConstantTokenLocal(body, insertPoint,
                    localName, booleanTokenClass, booleanTokenConstructor,
                    value);
            return tokenLocal;
        } else if (token instanceof DoubleToken) {
            Local tokenLocal = _buildConstantTokenLocal(body, insertPoint,
                    localName, doubleTokenClass, doubleTokenConstructor,
                    DoubleConstant.v(((DoubleToken) token).doubleValue()));
            return tokenLocal;
        } else if (token instanceof ComplexToken) {
            Complex complex = ((ComplexToken) token).complexValue();

            // ComplexToken takes a Complex as a constructor.
            SootClass complexClass = Scene.v().loadClassAndSupport(
                    "ptolemy.math.Complex");
            SootMethod complexConstructor = complexClass
                    .getMethod("void <init>(double,double)");

            Local complexLocal = Jimple.v().newLocal(localName + "Arg",
                    RefType.v(complexClass));
            body.getLocals().add(complexLocal);
            units.insertBefore(Jimple.v().newAssignStmt(complexLocal,
                    Jimple.v().newNewExpr(RefType.v(complexClass))),
                    insertPoint);
            units.insertBefore(Jimple.v().newInvokeStmt(
                    Jimple.v().newSpecialInvokeExpr(complexLocal,
                            complexConstructor.makeRef(),
                            DoubleConstant.v(complex.real),
                            DoubleConstant.v(complex.imag))), insertPoint);

            Local tokenLocal = _buildConstantTokenLocal(body, insertPoint,
                    localName, complexTokenClass, complexTokenConstructor,
                    complexLocal);
            return tokenLocal;
        } else if (token instanceof StringToken) {
            Local tokenLocal = _buildConstantTokenLocal(body, insertPoint,
                    localName, stringTokenClass, stringTokenConstructor,
                    StringConstant.v(((StringToken) token).stringValue()));
            return tokenLocal;
        } else if (token instanceof FixToken) {
            FixToken fixToken = (FixToken) token;
            FixPoint fixValue = fixToken.fixValue();
            List args = new ArrayList(3);

            // Some possible loss of precision?
            args.add(DoubleConstant.v(fixValue.doubleValue()));
            args.add(IntConstant.v(fixValue.getPrecision().getNumberOfBits()));
            args.add(IntConstant.v(fixValue.getPrecision()
                    .getIntegerBitLength()));

            Local tokenLocal = _buildConstantTokenLocal(body, insertPoint,
                    localName, fixTokenClass, fixTokenThreeArgConstructor, args);
            return tokenLocal;
        } else if (token instanceof FloatToken) {
            Local tokenLocal = _buildConstantTokenLocal(body, insertPoint,
                    localName, floatTokenClass, floatTokenConstructor,
                    FloatConstant.v(((FloatToken) token).floatValue()));
            return tokenLocal;
        } else if (token instanceof FunctionToken) {
            // Function tokens are partially supported, but cannot be
            // type specialized.  This can be folded into the case
            // below, if you are interested in trying it.
            throw new RuntimeException(
                    "Unboxing is not supported for FunctionTokens.");
        } else if (token instanceof MatrixToken) {
            // Can't do this for all tokens, because it causes an
            // infinite loop!
            String expression = token.toString();
            Local tokenLocal = DataUtilities.generateExpressionCodeBefore(null,
                    null, expression, new HashMap(), new HashMap(), body,
                    insertPoint);
            return tokenLocal;
        } else {
            // We want to avoid doing this for all tokens, because
            // string constructors are expensive..
            SootClass tokenClass = Scene.v().loadClassAndSupport(
                    token.getClass().getName());
            SootMethod tokenConstructor = tokenClass
                    .getMethod("void <init>(java.lang.String)");
            Local tokenLocal = Jimple.v().newLocal(localName,
                    RefType.v(tokenClass));
            body.getLocals().add(tokenLocal);
            units.insertBefore(Jimple.v().newAssignStmt(tokenLocal,
                    Jimple.v().newNewExpr(RefType.v(tokenClass))), insertPoint);
            units.insertBefore(Jimple.v().newInvokeStmt(
                    Jimple.v().newSpecialInvokeExpr(tokenLocal,
                            tokenConstructor.makeRef(),
                            StringConstant.v(token.toString()))), insertPoint);
            return tokenLocal;
        }
View Full Code Here

Examples of soot.util.Chain

     */

    // FIXME Records!
    public static Local buildConstantTypeLocal(Body body, Object insertPoint,
            ptolemy.data.type.Type type) {
        Chain units = body.getUnits();

        if (type instanceof ptolemy.data.type.BaseType
                || type instanceof ptolemy.data.type.MatrixType) {
            Local typeLocal = Jimple.v().newLocal("type_" + type.toString(),
                    RefType.v(baseTypeClass));
            body.getLocals().add(typeLocal);

            // This may look ugly, but wherever we insert type casts
            // it is more efficient and also much easier to optimize
            // during the translation process if we want to inline code.
            if (type.equals(ptolemy.data.type.BaseType.UNKNOWN)) {
                units.insertBefore(
                        Jimple.v().newAssignStmt(
                                typeLocal,
                                Jimple.v().newStaticFieldRef(
                                        unknownTypeField.makeRef())),
                        insertPoint);
            } else if (type.equals(ptolemy.data.type.BaseType.GENERAL)) {
                units.insertBefore(
                        Jimple.v().newAssignStmt(
                                typeLocal,
                                Jimple.v().newStaticFieldRef(
                                        generalTypeField.makeRef())),
                        insertPoint);
            } else if (type.equals(ptolemy.data.type.BaseType.BOOLEAN)) {
                units.insertBefore(
                        Jimple.v().newAssignStmt(
                                typeLocal,
                                Jimple.v().newStaticFieldRef(
                                        booleanTypeField.makeRef())),
                        insertPoint);
            } else if (type.equals(ptolemy.data.type.BaseType.BOOLEAN_MATRIX)) {
                units
                        .insertBefore(Jimple.v().newAssignStmt(
                                typeLocal,
                                Jimple.v().newStaticFieldRef(
                                        booleanMatrixTypeField.makeRef())),
                                insertPoint);
            } else if (type.equals(ptolemy.data.type.BaseType.COMPLEX)) {
                units.insertBefore(
                        Jimple.v().newAssignStmt(
                                typeLocal,
                                Jimple.v().newStaticFieldRef(
                                        complexTypeField.makeRef())),
                        insertPoint);
            } else if (type.equals(ptolemy.data.type.BaseType.COMPLEX_MATRIX)) {
                units
                        .insertBefore(Jimple.v().newAssignStmt(
                                typeLocal,
                                Jimple.v().newStaticFieldRef(
                                        complexMatrixTypeField.makeRef())),
                                insertPoint);
            } else if (type.equals(ptolemy.data.type.BaseType.DOUBLE)) {
                units.insertBefore(Jimple.v()
                        .newAssignStmt(
                                typeLocal,
                                Jimple.v().newStaticFieldRef(
                                        doubleTypeField.makeRef())),
                        insertPoint);
            } else if (type.equals(ptolemy.data.type.BaseType.DOUBLE_MATRIX)) {
                units.insertBefore(Jimple.v().newAssignStmt(
                        typeLocal,
                        Jimple.v().newStaticFieldRef(
                                doubleMatrixTypeField.makeRef())), insertPoint);
            } else if (type.equals(ptolemy.data.type.BaseType.FIX)) {
                units.insertBefore(Jimple.v().newAssignStmt(typeLocal,
                        Jimple.v().newStaticFieldRef(fixTypeField.makeRef())),
                        insertPoint);
            } else if (type.equals(ptolemy.data.type.BaseType.FIX_MATRIX)) {
                units.insertBefore(Jimple.v().newAssignStmt(
                        typeLocal,
                        Jimple.v().newStaticFieldRef(
                                fixMatrixTypeField.makeRef())), insertPoint);
            } else if (type.equals(ptolemy.data.type.BaseType.FLOAT)) {
                units
                        .insertBefore(Jimple.v().newAssignStmt(
                                typeLocal,
                                Jimple.v().newStaticFieldRef(
                                        floatTypeField.makeRef())), insertPoint);
            } else if (type.equals(ptolemy.data.type.BaseType.UNSIGNED_BYTE)) {
                units.insertBefore(Jimple.v().newAssignStmt(typeLocal,
                        Jimple.v().newStaticFieldRef(byteTypeField.makeRef())),
                        insertPoint);
            } else if (type.equals(ptolemy.data.type.BaseType.INT)) {
                units.insertBefore(Jimple.v().newAssignStmt(typeLocal,
                        Jimple.v().newStaticFieldRef(intTypeField.makeRef())),
                        insertPoint);
            } else if (type.equals(ptolemy.data.type.BaseType.INT_MATRIX)) {
                units.insertBefore(Jimple.v().newAssignStmt(
                        typeLocal,
                        Jimple.v().newStaticFieldRef(
                                intMatrixTypeField.makeRef())), insertPoint);
            } else if (type.equals(ptolemy.data.type.BaseType.LONG)) {
                units.insertBefore(Jimple.v().newAssignStmt(typeLocal,
                        Jimple.v().newStaticFieldRef(longTypeField.makeRef())),
                        insertPoint);
            } else if (type.equals(ptolemy.data.type.BaseType.LONG_MATRIX)) {
                units.insertBefore(Jimple.v().newAssignStmt(
                        typeLocal,
                        Jimple.v().newStaticFieldRef(
                                longMatrixTypeField.makeRef())), insertPoint);
            } else if (type.equals(ptolemy.data.type.BaseType.OBJECT)) {
                units.insertBefore(Jimple.v()
                        .newAssignStmt(
                                typeLocal,
                                Jimple.v().newStaticFieldRef(
                                        objectTypeField.makeRef())),
                        insertPoint);
            } else if (type.equals(ptolemy.data.type.BaseType.SHORT)) {
                units
                        .insertBefore(Jimple.v().newAssignStmt(
                                typeLocal,
                                Jimple.v().newStaticFieldRef(
                                        shortTypeField.makeRef())), insertPoint);
            } else if (type.equals(ptolemy.data.type.BaseType.STRING)) {
                units.insertBefore(Jimple.v()
                        .newAssignStmt(
                                typeLocal,
                                Jimple.v().newStaticFieldRef(
                                        stringTypeField.makeRef())),
                        insertPoint);
            } else {
                // Some base type that we didn't special case above.
                SootMethod typeConstructor = SootUtilities
                        .searchForMethodByName(baseTypeClass, "forName");
                units.insertBefore(Jimple.v().newAssignStmt(
                        typeLocal,
                        Jimple.v().newStaticInvokeExpr(
                                typeConstructor.makeRef(),
                                StringConstant.v(type.toString()))),
                        insertPoint);
            }

            return typeLocal;
        } else if (type instanceof ptolemy.data.type.ArrayType) {
            // recurse
            //SootMethod typeConstructor = SootUtilities.searchForMethodByName(
            //        arrayTypeClass, "<init>");
            // The array changes resulted in ArrayType having two
            // constructors: ArrayType(Type) and ArrayType(Type, int)
            SootMethod typeConstructor = arrayTypeClass
                    .getMethod("void <init>(ptolemy.data.type.Type)");

            Local elementTypeLocal = buildConstantTypeLocal(body, insertPoint,
                    ((ptolemy.data.type.ArrayType) type).getElementType());
            Local typeLocal = Jimple.v().newLocal(
                    "type_arrayOf" + elementTypeLocal.getName(),
                    RefType.v(arrayTypeClass));
            body.getLocals().add(typeLocal);
            units.insertBefore(Jimple.v().newAssignStmt(typeLocal,
                    Jimple.v().newNewExpr(RefType.v(arrayTypeClass))),
                    insertPoint);
            units.insertBefore(Jimple.v().newInvokeStmt(
                    Jimple.v().newSpecialInvokeExpr(typeLocal,
                            typeConstructor.makeRef(), elementTypeLocal)),
                    insertPoint);
            return typeLocal;
        } else if (type instanceof ptolemy.data.type.RecordType) {
            ptolemy.data.type.RecordType recordType = (ptolemy.data.type.RecordType) type;

            // recurse
            StringBuffer typeName = new StringBuffer("type_recordOf");

            // Create the new array of labels.
            Local labelArrayLocal = Jimple.v().newLocal("labelArray",
                    ArrayType.v(RefType.v("java.lang.String"), 1));
            body.getLocals().add(labelArrayLocal);
            units.insertBefore(Jimple.v().newAssignStmt(
                    labelArrayLocal,
                    Jimple.v().newNewArrayExpr(RefType.v("java.lang.String"),
                            IntConstant.v(recordType.labelSet().size()))),
                    insertPoint);

            // Create the new array of types.
            Local typeArrayLocal = Jimple.v().newLocal("typeArray",
                    ArrayType.v(RefType.v(typeClass), 1));
            body.getLocals().add(typeArrayLocal);
            units.insertBefore(Jimple.v().newAssignStmt(
                    typeArrayLocal,
                    Jimple.v().newNewArrayExpr(RefType.v(typeClass),
                            IntConstant.v(recordType.labelSet().size()))),
                    insertPoint);

            int count = 0;

            for (Iterator labels = recordType.labelSet().iterator(); labels
                    .hasNext(); count++) {
                String label = (String) labels.next();
                ptolemy.data.type.Type elementType = recordType.get(label);
                Local elementTypeLocal = buildConstantTypeLocal(body,
                        insertPoint, elementType);
                typeName.append("_" + label + "_" + elementTypeLocal.getName());

                // Store into the array of labels.
                units.insertBefore(
                        Jimple.v().newAssignStmt(
                                Jimple.v().newArrayRef(labelArrayLocal,
                                        IntConstant.v(count)),
                                StringConstant.v(label)), insertPoint);

                // Store into the array of types.
                units.insertBefore(Jimple.v().newAssignStmt(
                        Jimple.v().newArrayRef(typeArrayLocal,
                                IntConstant.v(count)), elementTypeLocal),
                        insertPoint);
            }

            // Create the new local and assign to local variable.
            Local typeLocal = Jimple.v().newLocal(typeName.toString(),
                    RefType.v(recordTypeClass));
            body.getLocals().add(typeLocal);
            units.insertBefore(Jimple.v().newAssignStmt(typeLocal,
                    Jimple.v().newNewExpr(RefType.v(recordTypeClass))),
                    insertPoint);

            // invoke the initializer.
            SootMethod typeConstructor = SootUtilities.searchForMethodByName(
                    recordTypeClass, "<init>");
            System.out.println("typeConstructor = " + typeConstructor);
            units.insertBefore(Jimple.v().newInvokeStmt(
                    Jimple.v().newSpecialInvokeExpr(typeLocal,
                            typeConstructor.makeRef(), labelArrayLocal,
                            typeArrayLocal)), insertPoint);
            return typeLocal;
        } else if (type instanceof ptolemy.data.type.FixType) {
            Local typeLocal = Jimple.v().newLocal("type_fix",
                    RefType.v(fixTypeClass));
            body.getLocals().add(typeLocal);
            units.insertBefore(Jimple.v().newAssignStmt(typeLocal,
                    Jimple.v().newStaticFieldRef(fixTypeField.makeRef())),
                    insertPoint);
            return typeLocal;
        } else if (type instanceof ptolemy.data.type.FunctionType) {
            ptolemy.data.type.FunctionType functionType = (ptolemy.data.type.FunctionType) type;

            // recurse
            String typeName = "type_function";

            // Create the new array of types.
            Local typeArrayLocal = Jimple.v().newLocal("typeArray",
                    ArrayType.v(RefType.v(typeClass), 1));
            body.getLocals().add(typeArrayLocal);
            units.insertBefore(Jimple.v().newAssignStmt(
                    typeArrayLocal,
                    Jimple.v().newNewArrayExpr(RefType.v(typeClass),
                            IntConstant.v(functionType.getArgCount()))),
                    insertPoint);

            for (int i = 0; i < functionType.getArgCount(); i++) {
                ptolemy.data.type.Type elementType = functionType.getArgType(i);
                Local elementTypeLocal = buildConstantTypeLocal(body,
                        insertPoint, elementType);

                // Store into the array of types.
                units.insertBefore(Jimple.v().newAssignStmt(
                        Jimple.v()
                                .newArrayRef(typeArrayLocal, IntConstant.v(i)),
                        elementTypeLocal), insertPoint);
            }

            Local returnTypeLocal = buildConstantTypeLocal(body, insertPoint,
                    functionType.getReturnType());

            // Create the new local and assign to local variable.
            Local typeLocal = Jimple.v().newLocal(typeName,
                    RefType.v(functionTypeClass));
            body.getLocals().add(typeLocal);
            units.insertBefore(Jimple.v().newAssignStmt(typeLocal,
                    Jimple.v().newNewExpr(RefType.v(functionTypeClass))),
                    insertPoint);

            // invoke the initializer.
            SootMethod typeConstructor = SootUtilities.searchForMethodByName(
                    functionTypeClass, "<init>");
            System.out.println("typeConstructor = " + typeConstructor);
            units.insertBefore(Jimple.v().newInvokeStmt(
                    Jimple.v().newSpecialInvokeExpr(typeLocal,
                            typeConstructor.makeRef(), typeArrayLocal,
                            returnTypeLocal)), insertPoint);
            return typeLocal;
        }
View Full Code Here

Examples of soot.util.Chain

     *  of the local to the created instance.
     *  @return The local that was created.
     */
    public static Local createNamedObjAndLocal(Body body, String className,
            Local container, String name) {
        Chain units = body.getUnits();
        SootClass objectClass;

        if (Scene.v().containsClass(className)) {
            objectClass = Scene.v().getSootClass(className);
        } else {
            objectClass = Scene.v().loadClassAndSupport(className);
        }

        // System.out.println("done loading support of " + className);
        RefType objectType = RefType.v(objectClass);

        // Create the new local with the given name.
        Local local = Jimple.v().newLocal(name, objectType);

        // Add the local to the body.
        body.getLocals().add(local);

        // Create the new local with the given name.
        Local attributeLocal = Jimple.v().newLocal(name, attributeType);

        // Add the local to the body.
        body.getLocals().add(attributeLocal);

        // Create the object.
        units.add(Jimple.v().newAssignStmt(local,
                Jimple.v().newNewExpr(objectType)));

        // The constructor arguments.
        List args = new LinkedList();
        args.add(container);
        args.add(StringConstant.v(name));

        // Call the constructor on the object.
        SootMethod constructor = SootUtilities.getMatchingMethod(objectClass,
                "<init>", args);

        if (constructor == null) {
            throw new RuntimeException("Could not find 2 argument constructor"
                    + " for class " + objectClass + "( " + container + ", \""
                    + name + "\")");
        }

        units.add(Jimple.v().newInvokeStmt(
                Jimple.v().newSpecialInvokeExpr(local, constructor.makeRef(),
                        args)));
        return local;
    }
View Full Code Here

Examples of soot.util.Chain

        // 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)));

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

Examples of soot.util.Chain

                            continue;
                        }

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

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

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

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

                        initUnits.insertBefore(Jimple.v().newAssignStmt(
                                arrayLocal,
                                Jimple.v().newNewArrayExpr(tokenType,
                                        IntConstant.v(bufferSize))),
                                insertPoint);
                        initUnits.insertBefore(Jimple.v().newAssignStmt(
                                Jimple.v().newInstanceFieldRef(containerLocal,
                                        field.makeRef()), arrayLocal),
                                insertPoint);
                    }
                }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.