Package soot

Examples of soot.Local


                                        + unit);
                            }

                            // Handle ArrayType.getElementType specially.
                            if (baseTokenType instanceof ptolemy.data.type.ArrayType) {
                                Local local = PtolemyUtilities
                                        .buildConstantTypeLocal(
                                                body,
                                                unit,
                                                ((ptolemy.data.type.ArrayType) baseTokenType)
                                                        .getElementType());
View Full Code Here


            // local variable that will replace a
            // fieldReference to that field based on the
            // local.
            for (Iterator locals = body.getLocals().snapshotIterator(); locals
                    .hasNext();) {
                Local local = (Local) locals.next();
                Type localType = typeAnalysis.getSpecializedSootType(local);

                if (debug) {
                    System.out
                            .println("Attempting to create replacement fields for local = "
                                    + local);
                }

                if (debug) {
                    System.out.println("Type = " + localType);
                }

                // If the type is not a token, then skip it.
                if (!PtolemyUtilities.isConcreteTokenType(localType)
                        || unsafeLocalSet.contains(local)) {
                    if (debug) {
                        System.out
                                .println("skipping: unsafe or not concrete token");
                    }

                    continue;
                }

                ptolemy.data.type.Type localTokenType = typeAnalysis
                        .getSpecializedType(local);

                if (debug) {
                    System.out.println("localTokenType = " + localTokenType);
                }

                // Ignore fields that aren't of the right depth.
                if (PtolemyUtilities.getTypeDepth(localTokenType) != depth) {
                    if (debug) {
                        System.out
                                .println("skipping: depth is only "
                                        + PtolemyUtilities
                                                .getTypeDepth(localTokenType));
                    }

                    continue;
                }

                // If the type is not instantiable, then skip it.
                //    if (!localTokenType.isInstantiable()) {
                //    continue;
                // }
                // If we've already created subfields for this
                // field, then don't do it again.
                if (localToFieldToLocal.get(local) != null) {
                    continue;
                }

                RefType type = PtolemyUtilities.getBaseTokenType(localType);
                SootClass localClass = type.getSootClass();

                if (!SootUtilities.derivesFrom(localClass,
                        PtolemyUtilities.tokenClass)) {
                    if (debug) {
                        System.out.println("skipping: not a token.");
                    }

                    continue;
                }

                if (debug) {
                    System.out
                            .println("Creating replacement fields for local = "
                                    + local);
                }

                if (debug) {
                    System.out.println("localClass = " + localClass);
                }

                // We are going to make a modification
                doneSomething = true;

                Type isNotNullType = SootUtilities.createIsomorphicType(
                        localType, BooleanType.v());

                // Create a boolean value that tells us whether or
                // not the token is null.  Initialize it to true.
                Local isNotNullLocal = Jimple.v().newLocal(
                        local.getName() + "_isNotNull", isNotNullType);
                body.getLocals().add(isNotNullLocal);
                localToIsNotNullLocal.put(local, isNotNullLocal);

                // Note: default initialization is to false..
                //                 body.getUnits().insertBefore(
                //                         Jimple.v().newAssignStmt(
                //                                 isNotNullLocal,
                //                                 IntConstant.v(1)),
                //                         body.getFirstNonIdentityStmt());
                Map tokenFieldToReplacementLocal = new HashMap();
                localToFieldToLocal.put(local, tokenFieldToReplacementLocal);

                for (Iterator tokenFields = _getTokenClassFields(localClass)
                        .iterator(); tokenFields.hasNext();) {
                    SootField tokenField = (SootField) tokenFields.next();

                    if (debug) {
                        System.out.println("tokenField = " + tokenField);
                    }

                    Type replacementType = SootUtilities.createIsomorphicType(
                            localType, tokenField.getType());
                    Local replacementLocal = Jimple.v().newLocal(
                            local.getName() + "_" + tokenField.getName(),
                            replacementType);
                    body.getLocals().add(replacementLocal);
                    tokenFieldToReplacementLocal.put(tokenField,
                            replacementLocal);
                }
            }

            // Go back again and replace references to fields
            // in the token with references to local
            // variables.
            for (Iterator units = body.getUnits().snapshotIterator(); units
                    .hasNext();) {
                Unit unit = (Unit) units.next();

                if (debug) {
                    System.out.println("ttn2 unit = " + unit);
                }

                if (unit instanceof InvokeStmt) {
                    // Handle java.lang.arraycopy
                    InvokeExpr r = (InvokeExpr) ((InvokeStmt) unit)
                            .getInvokeExpr();

                    if (r.getMethod().equals(PtolemyUtilities.arraycopyMethod)) {
                        if (debug) {
                            System.out.println("handling as array copy");
                        }

                        Local toLocal = (Local) r.getArg(0);
                        Local fromLocal = (Local) r.getArg(2);
                        Map toFieldToReplacementLocal = (Map) localToFieldToLocal
                                .get(toLocal);
                        Map fromFieldToReplacementLocal = (Map) localToFieldToLocal
                                .get(fromLocal);

                        if ((toFieldToReplacementLocal != null)
                                && (fromFieldToReplacementLocal != null)) {
                            if (debug) {
                                System.out
                                        .println("toFieldToReplacementLocal = "
                                                + toFieldToReplacementLocal);
                            }

                            if (debug) {
                                System.out
                                        .println("fromFieldToReplacementLocal = "
                                                + fromFieldToReplacementLocal);
                            }

                            {
                                List argumentList = new LinkedList();
                                argumentList.add((Local) localToIsNotNullLocal
                                        .get(toLocal));
                                argumentList.add(r.getArg(1));
                                argumentList.add((Local) localToIsNotNullLocal
                                        .get(fromLocal));
                                argumentList.add(r.getArg(3));
                                argumentList.add(r.getArg(4));

                                body
                                        .getUnits()
                                        .insertBefore(
                                                Jimple
                                                        .v()
                                                        .newInvokeStmt(
                                                                Jimple
                                                                        .v()
                                                                        .newStaticInvokeExpr(
                                                                                PtolemyUtilities.arraycopyMethod
                                                                                        .makeRef(),
                                                                                argumentList)),
                                                unit);
                            }

                            for (Iterator tokenFields = toFieldToReplacementLocal
                                    .keySet().iterator(); tokenFields.hasNext();) {
                                SootField tokenField = (SootField) tokenFields
                                        .next();

                                Local toReplacementLocal = (Local) toFieldToReplacementLocal
                                        .get(tokenField);
                                Local fromReplacementLocal = (Local) fromFieldToReplacementLocal
                                        .get(tokenField);
                                List argumentList = new LinkedList();
                                argumentList.add(toReplacementLocal);
                                argumentList.add(r.getArg(1));
                                argumentList.add(fromReplacementLocal);
                                argumentList.add(r.getArg(3));
                                argumentList.add(r.getArg(4));

                                body
                                        .getUnits()
                                        .insertBefore(
                                                Jimple
                                                        .v()
                                                        .newInvokeStmt(
                                                                Jimple
                                                                        .v()
                                                                        .newStaticInvokeExpr(
                                                                                PtolemyUtilities.arraycopyMethod
                                                                                        .makeRef(),
                                                                                argumentList)),
                                                unit);
                            }

                            body.getUnits().remove(unit);
                            doneSomething = true;
                        }
                    }
                } else if (unit instanceof AssignStmt) {
                    AssignStmt stmt = (AssignStmt) unit;
                    /*Type assignmentType =*/stmt.getLeftOp().getType();

                    if (stmt.getLeftOp() instanceof Local
                            && stmt.getRightOp() instanceof LengthExpr) {
                        if (debug) {
                            System.out.println("handling as length expr");
                        }

                        LengthExpr lengthExpr = (LengthExpr) stmt.getRightOp();
                        Local baseLocal = (Local) lengthExpr.getOp();

                        if (debug) {
                            System.out.println("operating on " + baseLocal);
                        }

                        Map fieldToReplacementArrayLocal = (Map) localToFieldToLocal
                                .get(baseLocal);

                        if (fieldToReplacementArrayLocal != null) {
                            doneSomething = true;

                            // Get the length of a random one of the replacement fields.
                            List replacementList = new ArrayList(
                                    fieldToReplacementArrayLocal.keySet());
                            Collections.sort(replacementList, new Comparator() {
                                public int compare(Object o1, Object o2) {
                                    SootField f1 = (SootField) o1;
                                    SootField f2 = (SootField) o2;
                                    return f1.getName().compareTo(f2.getName());
                                }
                            });

                            SootField field = (SootField) replacementList
                                    .get(replacementList.size() - 1);

                            if (debug) {
                                System.out.println("replace with  "
                                        + fieldToReplacementArrayLocal
                                                .get(field));
                            }

                            lengthExpr
                                    .setOp((Local) fieldToReplacementArrayLocal
                                            .get(field));

                            if (debug) {
                                System.out.println("unit now = " + unit);
                            }

                            //    body.getUnits().remove(unit);
                        }
                    } else if (stmt.getLeftOp() instanceof InstanceFieldRef) {
                        // Replace references to fields of tokens.
                        // FIXME: assign to all aliases as well.
                        if (debug) {
                            System.out
                                    .println("is assignment to Instance FieldRef");
                        }

                        InstanceFieldRef r = (InstanceFieldRef) stmt
                                .getLeftOp();
                        SootField field = r.getField();

                        if (r.getBase().getType() instanceof RefType) {
                            RefType type = (RefType) r.getBase().getType();

                            //System.out.println("BaseType = " + type);
                            if (SootUtilities.derivesFrom(type.getSootClass(),
                                    PtolemyUtilities.tokenClass)) {
                                if (debug) {
                                    System.out.println("handling " + unit
                                            + " token operation");
                                }

                                // We have a reference to a field of a token class.
                                Local baseLocal = (Local) r.getBase();
                                Local instanceLocal = _getInstanceLocal(body,
                                        baseLocal, field, localToFieldToLocal,
                                        debug);

                                if (debug) {
                                    System.out.println("instanceLocal = "
                                            + instanceLocal);
                                }

                                if (instanceLocal != null) {
                                    stmt.getLeftOpBox().setValue(instanceLocal);
                                    doneSomething = true;
                                }
                            }
                        }
                    } else if (stmt.getRightOp() instanceof InstanceFieldRef) {
                        // Replace references to fields of tokens.
                        if (debug) {
                            System.out
                                    .println("is assignment from Instance FieldRef");
                        }

                        InstanceFieldRef r = (InstanceFieldRef) stmt
                                .getRightOp();
                        SootField field = r.getField();

                        if (r.getBase().getType() instanceof RefType) {
                            RefType type = (RefType) r.getBase().getType();

                            //System.out.println("BaseType = " + type);
                            if (SootUtilities.derivesFrom(type.getSootClass(),
                                    PtolemyUtilities.tokenClass)) {
                                if (debug) {
                                    System.out.println("handling " + unit
                                            + " token operation");
                                }

                                // We have a reference to a field of a token class.
                                Local baseLocal = (Local) r.getBase();
                                Local instanceLocal = _getInstanceLocal(body,
                                        baseLocal, field, localToFieldToLocal,
                                        debug);

                                if (debug) {
                                    System.out.println("instanceLocal = "
                                            + instanceLocal);
                                }

                                if (instanceLocal != null) {
                                    stmt.getRightOpBox()
                                            .setValue(instanceLocal);
                                    doneSomething = true;
                                }
                            }
                        }
                    }
                }
            }
        }

        if (debug) {
            System.out.println("Specializing types for " + entityClass);
        }

        // Specialize the token types.  Any field we created above
        // should now have its correct concrete type.
        // TypeSpecializer.specializeTypes(debug, entityClass, unsafeLocalSet);
        // Now go through the methods again and handle all token assignments,
        // replacing them with assignments on the native replacements.
        for (Iterator methods = entityClass.getMethods().iterator(); methods
                .hasNext();) {
            SootMethod method = (SootMethod) methods.next();

            if (debug) {
                System.out.println("Replacing token assignments in method "
                        + method);
            }

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

            //   InstanceEqualityEliminator.removeInstanceEqualities(body, null, true);
            for (Iterator units = body.getUnits().snapshotIterator(); units
                    .hasNext();) {
                Unit unit = (Unit) units.next();

                if (debug) {
                    System.out.println("ttn3 unit = " + unit);
                }

                // Hack to work around the presence of NIL fields:
                // Assume they are null references.
                //      for (Iterator boxes = unit.getUseAndDefBoxes().iterator(); boxes
                //                         .hasNext();) {
                //                     ValueBox box = (ValueBox) boxes.next();
                //                     Value value = box.getValue();
                //                     if (value instanceof FieldRef) {
                //                         FieldRef ref = (FieldRef)value;
                //                         SootField field = ref.getField();
                //                          if (field.getName().equals("NIL")) {
                //                              doneSomething = true;

                //                              box.setValue(NullConstant.v());
                //                              System.out.println("replacing as Null");
                //                          }
                //                     }
                //                 }

                if (unit instanceof AssignStmt) {
                    AssignStmt stmt = (AssignStmt) unit;
                    Type assignmentType = stmt.getLeftOp().getType();

                    if (PtolemyUtilities.isTokenType(assignmentType)) {
                        if (stmt.getLeftOp() instanceof Local
                                && (stmt.getRightOp() instanceof Local || stmt
                                        .getRightOp() instanceof Constant)) {
                            if (debug) {
                                System.out
                                        .println("handling as local-immediate assign");
                            }

                            doneSomething |= _handleImmediateAssignment(body,
                                    stmt, localToFieldToLocal,
                                    localToIsNotNullLocal, stmt.getLeftOp(),
                                    stmt.getRightOp(), debug);
                        } else if (stmt.getLeftOp() instanceof Local
                                && stmt.getRightOp() instanceof CastExpr) {
                            if (debug) {
                                System.out.println("handling as local cast");
                            }

                            Value rightLocal = ((CastExpr) stmt.getRightOp())
                                    .getOp();

                            doneSomething |= _handleImmediateAssignment(body,
                                    stmt, localToFieldToLocal,
                                    localToIsNotNullLocal, stmt.getLeftOp(),
                                    rightLocal, debug);
                        } else if (stmt.getLeftOp() instanceof FieldRef
                                && stmt.getRightOp() instanceof Local) {
                            if (debug) {
                                System.out
                                        .println("handling as assignment to Field");
                            }

                            FieldRef oldFieldRef = (FieldRef) stmt.getLeftOp();
                            SootField field = oldFieldRef.getField();
                            Map fieldToReplacementField = (Map) entityFieldToTokenFieldToReplacementField
                                    .get(field);
                            Map fieldToReplacementLocal = (Map) localToFieldToLocal
                                    .get(stmt.getRightOp());

                            //        System.out.println("fieldToReplacementField = " + fieldToReplacementField);
                            //                             System.out.println("fieldToReplacementLocal = " + fieldToReplacementLocal);
                            if ((fieldToReplacementLocal != null)
                                    && (fieldToReplacementField != null)) {
                                doneSomething = true;
                                // Replace references to fields with token types.
                                {
                                    SootField replacementField = (SootField) entityFieldToIsNotNullField
                                            .get(field);

                                    //System.out.println("replacementField = " + replacementField);
                                    FieldRef isNotNullFieldRef;

                                    if (oldFieldRef instanceof InstanceFieldRef) {
                                        isNotNullFieldRef = Jimple
                                                .v()
                                                .newInstanceFieldRef(
                                                        ((InstanceFieldRef) oldFieldRef)
                                                                .getBase(),
                                                        replacementField
                                                                .makeRef());
                                    } else {
                                        isNotNullFieldRef = Jimple.v()
                                                .newStaticFieldRef(
                                                        replacementField
                                                                .makeRef());
                                    }

                                    body
                                            .getUnits()
                                            .insertBefore(
                                                    Jimple
                                                            .v()
                                                            .newAssignStmt(
                                                                    isNotNullFieldRef,
                                                                    (Local) localToIsNotNullLocal
                                                                            .get(stmt
                                                                                    .getRightOp())),
                                                    unit);
                                }

                                if (debug) {
                                    System.out.println("local = "
                                            + stmt.getLeftOp());
                                }

                                for (Iterator tokenFields = fieldToReplacementField
                                        .keySet().iterator(); tokenFields
                                        .hasNext();) {
                                    SootField tokenField = (SootField) tokenFields
                                            .next();

                                    if (debug) {
                                        System.out.println("tokenField = "
                                                + tokenField);
                                    }

                                    SootField replacementField = (SootField) fieldToReplacementField
                                            .get(tokenField);
                                    Local replacementLocal = (Local) fieldToReplacementLocal
                                            .get(tokenField);

                                    if (debug) {
                                        System.out
                                                .println("replacementLocal = "
                                                        + replacementLocal);
                                    }

                                    FieldRef fieldRef;

                                    if (stmt.getLeftOp() instanceof InstanceFieldRef) {
                                        Local base = (Local) ((InstanceFieldRef) stmt
                                                .getLeftOp()).getBase();
                                        fieldRef = Jimple.v()
                                                .newInstanceFieldRef(
                                                        base,
                                                        replacementField
                                                                .makeRef());
                                    } else {
                                        fieldRef = Jimple.v()
                                                .newStaticFieldRef(
                                                        replacementField
                                                                .makeRef());
                                    }

                                    body.getUnits().insertBefore(
                                            Jimple.v().newAssignStmt(fieldRef,
                                                    replacementLocal), unit);
                                }

                                stmt.getRightOpBox().setValue(NullConstant.v());

                                // body.getUnits().remove(unit);
                            }
                        } else if (stmt.getLeftOp() instanceof Local
                                && stmt.getRightOp() instanceof FieldRef) {
                            if (debug) {
                                System.out
                                        .println("handling as assignment from Field");
                            }

                            FieldRef oldFieldRef = (FieldRef) stmt.getRightOp();
                            Map fieldToReplacementLocal = (Map) localToFieldToLocal
                                    .get(stmt.getLeftOp());
                            SootField field = oldFieldRef.getField();
                            Map fieldToReplacementField = (Map) entityFieldToTokenFieldToReplacementField
                                    .get(field);

                            // There are some fields that represent
                            // singleton tokens.  Deal with them
                            // specially.

                            // This is awkward, but it is simpler than
                            // doing a dataflow analysis of the static
                            // initializer of the token class to
                            // figure out what the value is.
                            boolean isSingleton = false;
                            if (field.getName().equals("TRUE")
                                    || field.getName().equals("FALSE")
                                    || field.getName().equals("ZERO")
                                    || field.getName().equals("ONE")) {
                                isSingleton = true;
                            }

                            if ((isSingleton)
                                    && (fieldToReplacementLocal != null)) {
                                doneSomething = true;

                                // Replace references to fields with
                                // token types.  The special fields
                                // should never be null
                                body.getUnits().insertBefore(
                                        Jimple.v().newAssignStmt(
                                                (Local) localToIsNotNullLocal
                                                        .get(stmt.getLeftOp()),
                                                IntConstant.v(1)), unit);

                                if (debug) {
                                    System.out.println("local = "
                                            + stmt.getLeftOp());
                                }

                                for (Iterator localFields = fieldToReplacementLocal
                                        .keySet().iterator(); localFields
                                        .hasNext();) {
                                    SootField localField = (SootField) localFields
                                            .next();

                                    if (debug) {
                                        System.out.println("localField = "
                                                + localField);
                                    }

                                    //     if (localField.getName().equals("_isNil")) {
                                    //                                         Local replacementLocal = (Local) fieldToReplacementLocal
                                    //                                                 .get(localField);

                                    //                                         body
                                    //                                                 .getUnits()
                                    //                                                 .insertBefore(
                                    //                                                         Jimple
                                    //                                                                 .v()
                                    //                                                                 .newAssignStmt(
                                    //                                                                         replacementLocal,
                                    //                                                                         IntConstant
                                    //                                                                                 .v(1)),
                                    //                                                         unit);
                                    //                                     } else
                                    if (localField.getName().equals(
                                            "_unitCategoryExponents")) {

                                        Local replacementLocal = (Local) fieldToReplacementLocal
                                                .get(localField);
                                        body
                                                .getUnits()
                                                .insertBefore(
                                                        Jimple
                                                                .v()
                                                                .newAssignStmt(
                                                                        replacementLocal,
                                                                        NullConstant
                                                                                .v()),
                                                        unit);
                                    } else if (localField.getName().equals(
                                            "_value")) {

                                        Local replacementLocal = (Local) fieldToReplacementLocal
                                                .get(localField);

                                        if (field
                                                .getSignature()
                                                .equals(
                                                        "<ptolemy.data.BooleanToken: ptolemy.data.BooleanToken TRUE>")) {
                                            body.getUnits().insertBefore(
                                                    Jimple.v().newAssignStmt(
                                                            replacementLocal,
                                                            IntConstant.v(1)),
                                                    unit);
                                        } else if (field
                                                .getSignature()
                                                .equals(
                                                        "<ptolemy.data.BooleanToken: ptolemy.data.BooleanToken FALSE>")) {

                                            body.getUnits().insertBefore(
                                                    Jimple.v().newAssignStmt(
                                                            replacementLocal,
                                                            IntConstant.v(0)),
                                                    unit);
                                        } else if (field
                                                .getSignature()
                                                .equals(
                                                        "<ptolemy.data.UnsignedByteToken: ptolemy.data.UnsignedByteToken ONE>")) {
                                            body.getUnits().insertBefore(
                                                    Jimple.v().newAssignStmt(
                                                            replacementLocal,
                                                            IntConstant.v(1)),
                                                    unit);
                                        } else if (field
                                                .getSignature()
                                                .equals(
                                                        "<ptolemy.data.UnsignedByteToken: ptolemy.data.UnsignedByteToken ZERO>")) {

                                            body.getUnits().insertBefore(
                                                    Jimple.v().newAssignStmt(
                                                            replacementLocal,
                                                            IntConstant.v(0)),
                                                    unit);
                                        } else if (field
                                                .getSignature()
                                                .equals(
                                                        "<ptolemy.data.IntToken: ptolemy.data.IntToken ONE>")) {
                                            body.getUnits().insertBefore(
                                                    Jimple.v().newAssignStmt(
                                                            replacementLocal,
                                                            IntConstant.v(1)),
                                                    unit);
                                        } else if (field
                                                .getSignature()
                                                .equals(
                                                        "<ptolemy.data.IntToken: ptolemy.data.IntToken ZERO>")) {

                                            body.getUnits().insertBefore(
                                                    Jimple.v().newAssignStmt(
                                                            replacementLocal,
                                                            IntConstant.v(0)),
                                                    unit);
                                        } else if (field
                                                .getSignature()
                                                .equals(
                                                        "<ptolemy.data.LongToken: ptolemy.data.LongToken ONE>")) {
                                            body.getUnits().insertBefore(
                                                    Jimple.v().newAssignStmt(
                                                            replacementLocal,
                                                            LongConstant.v(1)),
                                                    unit);
                                        } else if (field
                                                .getSignature()
                                                .equals(
                                                        "<ptolemy.data.LongToken: ptolemy.data.LongToken ZERO>")) {

                                            body.getUnits().insertBefore(
                                                    Jimple.v().newAssignStmt(
                                                            replacementLocal,
                                                            LongConstant.v(0)),
                                                    unit);
                                        } else if (field
                                                .getSignature()
                                                .equals(
                                                        "<ptolemy.data.DoubleToken: ptolemy.data.DoubleToken ONE>")) {

                                            body.getUnits().insertBefore(
                                                    Jimple.v().newAssignStmt(
                                                            replacementLocal,
                                                            DoubleConstant
                                                                    .v(1.0)),
                                                    unit);
                                        } else if (field
                                                .getSignature()
                                                .equals(
                                                        "<ptolemy.data.DoubleToken: ptolemy.data.DoubleToken ZERO>")) {

                                            body.getUnits().insertBefore(
                                                    Jimple.v().newAssignStmt(
                                                            replacementLocal,
                                                            DoubleConstant
                                                                    .v(0.0)),
                                                    unit);
                                        }
                                    } else {
                                        throw new RuntimeException(
                                                "Unknown Field in Token: "
                                                        + localField
                                                                .getSignature());
                                    }
                                }

                                stmt.getRightOpBox().setValue(NullConstant.v());

                                //body.getUnits().remove(unit);
                            } else if ((fieldToReplacementLocal != null)
                                    && (fieldToReplacementField != null)) {
                                doneSomething = true;
                                // Replace references to fields with token types.
                                // FIXME properly handle isNotNull field?
                                {
                                    SootField replacementField = (SootField) entityFieldToIsNotNullField
                                            .get(field);

                                    //   System.out.println("replacementField = " + replacementField);
                                    FieldRef isNotNullFieldRef;

                                    if (oldFieldRef instanceof InstanceFieldRef) {
                                        isNotNullFieldRef = Jimple
                                                .v()
                                                .newInstanceFieldRef(
                                                        ((InstanceFieldRef) oldFieldRef)
                                                                .getBase(),
                                                        replacementField
                                                                .makeRef());
                                    } else {
                                        isNotNullFieldRef = Jimple.v()
                                                .newStaticFieldRef(
                                                        replacementField
                                                                .makeRef());
                                    }

                                    body
                                            .getUnits()
                                            .insertBefore(
                                                    Jimple
                                                            .v()
                                                            .newAssignStmt(
                                                                    (Local) localToIsNotNullLocal
                                                                            .get(stmt
                                                                                    .getLeftOp()),
                                                                    isNotNullFieldRef),
                                                    unit);
                                }

                                if (debug) {
                                    System.out.println("local = "
                                            + stmt.getLeftOp());
                                }

                                for (Iterator tokenFields = fieldToReplacementField
                                        .keySet().iterator(); tokenFields
                                        .hasNext();) {
                                    SootField tokenField = (SootField) tokenFields
                                            .next();

                                    if (debug) {
                                        System.out.println("tokenField = "
                                                + tokenField);
                                    }

                                    SootField replacementField = (SootField) fieldToReplacementField
                                            .get(tokenField);
                                    Local replacementLocal = (Local) fieldToReplacementLocal
                                            .get(tokenField);
                                    FieldRef fieldRef;

                                    if (stmt.getRightOp() instanceof InstanceFieldRef) {
                                        Local base = (Local) ((InstanceFieldRef) stmt
                                                .getRightOp()).getBase();
                                        fieldRef = Jimple.v()
                                                .newInstanceFieldRef(
                                                        base,
                                                        replacementField
                                                                .makeRef());
                                    } else {
                                        fieldRef = Jimple.v()
                                                .newStaticFieldRef(
                                                        replacementField
                                                                .makeRef());
                                    }

                                    if (debug) {
                                        System.out
                                                .println("replacementLocal = "
                                                        + replacementLocal);
                                    }

                                    body.getUnits()
                                            .insertBefore(
                                                    Jimple.v().newAssignStmt(
                                                            replacementLocal,
                                                            fieldRef), unit);
                                }

                                stmt.getRightOpBox().setValue(NullConstant.v());

                                //      body.getUnits().remove(unit);
                            }
                        } else if (stmt.getLeftOp() instanceof ArrayRef
                                && stmt.getRightOp() instanceof Local) {
                            if (debug) {
                                System.out
                                        .println("handling as assignment to Array");
                            }

                            ArrayRef arrayRef = (ArrayRef) stmt.getLeftOp();
                            Local baseLocal = (Local) arrayRef.getBase();
                            Map fieldToReplacementArrayLocal = (Map) localToFieldToLocal
                                    .get(baseLocal);
                            Map fieldToReplacementLocal = (Map) localToFieldToLocal
                                    .get(stmt.getRightOp());

                            if (debug) {
                                System.out
                                        .println("fieldToReplacementArrayLocal = "
                                                + fieldToReplacementArrayLocal);
                            }

                            if (debug) {
                                System.out.println("fieldToReplacementLocal = "
                                        + fieldToReplacementLocal);
                            }

                            if ((fieldToReplacementLocal != null)
                                    && (fieldToReplacementArrayLocal != null)) {
                                doneSomething = true;
                                body
                                        .getUnits()
                                        .insertBefore(
                                                Jimple
                                                        .v()
                                                        .newAssignStmt(
                                                                Jimple
                                                                        .v()
                                                                        .newArrayRef(
                                                                                (Local) localToIsNotNullLocal
                                                                                        .get(baseLocal),
                                                                                arrayRef
                                                                                        .getIndex()),
                                                                (Local) localToIsNotNullLocal
                                                                        .get(stmt
                                                                                .getRightOp())),
                                                unit);

                                if (debug) {
                                    System.out.println("local = "
                                            + stmt.getLeftOp());
                                }

                                for (Iterator tokenFields = fieldToReplacementLocal
                                        .keySet().iterator(); tokenFields
                                        .hasNext();) {
                                    SootField tokenField = (SootField) tokenFields
                                            .next();

                                    if (debug) {
                                        System.out.println("tokenField = "
                                                + tokenField);
                                    }

                                    Local replacementArrayLocal = (Local) fieldToReplacementArrayLocal
                                            .get(tokenField);
                                    Local replacementLocal = (Local) fieldToReplacementLocal
                                            .get(tokenField);

                                    body
                                            .getUnits()
                                            .insertBefore(
                                                    Jimple
                                                            .v()
                                                            .newAssignStmt(
                                                                    Jimple
                                                                            .v()
                                                                            .newArrayRef(
                                                                                    replacementArrayLocal,
                                                                                    arrayRef
                                                                                            .getIndex()),
                                                                    replacementLocal),
                                                    unit);
                                }

                                // Have to remove here, because otherwise we'll try to
                                // index into a null array.
                                //stmt.getRightOpBox().setValue(NullConstant.v());
                                body.getUnits().remove(unit);
                            }
                        } else if (stmt.getLeftOp() instanceof Local
                                && stmt.getRightOp() instanceof ArrayRef) {
                            if (debug) {
                                System.out
                                        .println("handling as assignment from Array");
                            }

                            ArrayRef arrayRef = (ArrayRef) stmt.getRightOp();
                            Map fieldToReplacementLocal = (Map) localToFieldToLocal
                                    .get(stmt.getLeftOp());
                            Local baseLocal = (Local) arrayRef.getBase();
                            Map fieldToReplacementArrayLocal = (Map) localToFieldToLocal
                                    .get(baseLocal);

                            if ((fieldToReplacementLocal != null)
                                    && (fieldToReplacementArrayLocal != null)) {
                                doneSomething = true;

                                body
                                        .getUnits()
                                        .insertBefore(
                                                Jimple
                                                        .v()
                                                        .newAssignStmt(
                                                                (Local) localToIsNotNullLocal
                                                                        .get(stmt
                                                                                .getLeftOp()),
                                                                Jimple
                                                                        .v()
                                                                        .newArrayRef(
                                                                                (Local) localToIsNotNullLocal
                                                                                        .get(baseLocal),
                                                                                arrayRef
                                                                                        .getIndex())),
                                                unit);

                                if (debug) {
                                    System.out.println("local = "
                                            + stmt.getLeftOp());
                                }

                                for (Iterator tokenFields = fieldToReplacementLocal
                                        .keySet().iterator(); tokenFields
                                        .hasNext();) {
                                    SootField tokenField = (SootField) tokenFields
                                            .next();

                                    if (debug) {
                                        System.out.println("tokenField = "
                                                + tokenField);
                                    }

                                    Local replacementArrayLocal = (Local) fieldToReplacementArrayLocal
                                            .get(tokenField);
                                    Local replacementLocal = (Local) fieldToReplacementLocal
                                            .get(tokenField);

                                    if (debug) {
                                        System.out
                                                .println("replacementLocal = "
                                                        + replacementLocal);
                                    }

                                    if (debug) {
                                        System.out
                                                .println("replacementArrayLocal = "
                                                        + replacementArrayLocal);
                                    }

                                    body
                                            .getUnits()
                                            .insertBefore(
                                                    Jimple
                                                            .v()
                                                            .newAssignStmt(
                                                                    replacementLocal,
                                                                    Jimple
                                                                            .v()
                                                                            .newArrayRef(
                                                                                    replacementArrayLocal,
                                                                                    arrayRef
                                                                                            .getIndex())),
                                                    unit);
                                }

                                stmt.getRightOpBox().setValue(NullConstant.v());

                                //body.getUnits().remove(unit);
                            }
                        } else if (stmt.getLeftOp() instanceof Local
                                && stmt.getRightOp() instanceof NewArrayExpr) {
                            if (debug) {
                                System.out
                                        .println("handling as new array object");
                            }

                            NewArrayExpr newExpr = (NewArrayExpr) stmt
                                    .getRightOp();

                            // We have an assignment to a local from a new array.
                            Map map = (Map) localToFieldToLocal.get(stmt
                                    .getLeftOp());

                            if (map != null) {
                                doneSomething = true;

                                Type isNotNullType = SootUtilities
                                        .createIsomorphicType(newExpr
                                                .getBaseType(), BooleanType.v());
                                Local isNotNullLocal = (Local) localToIsNotNullLocal
                                        .get(stmt.getLeftOp());
                                body.getUnits().insertBefore(
                                        Jimple.v().newAssignStmt(
                                                isNotNullLocal,
                                                Jimple.v().newNewArrayExpr(
                                                        isNotNullType,
                                                        newExpr.getSize())),
                                        unit);

                                for (Iterator tokenFields = map.keySet()
                                        .iterator(); tokenFields.hasNext();) {
                                    SootField tokenField = (SootField) tokenFields
                                            .next();

                                    if (debug) {
                                        System.out.println("tokenField = "
                                                + tokenField);
                                    }

                                    Local replacementLocal = (Local) map
                                            .get(tokenField);

                                    Type replacementType = SootUtilities
                                            .createIsomorphicType(newExpr
                                                    .getBaseType(), tokenField
                                                    .getType());

                                    // Initialize fields?
                                    body
                                            .getUnits()
                                            .insertBefore(
                                                    Jimple
                                                            .v()
                                                            .newAssignStmt(
                                                                    replacementLocal,
                                                                    Jimple
                                                                            .v()
                                                                            .newNewArrayExpr(
                                                                                    replacementType,
                                                                                    newExpr
                                                                                            .getSize())),
                                                    unit);
                                }

                                stmt.getRightOpBox().setValue(NullConstant.v());

                                //body.getUnits().remove(unit);
                            }
                        } else if (stmt.getLeftOp() instanceof Local
                                && stmt.getRightOp() instanceof NewExpr) {
                            if (debug) {
                                System.out.println("handling as new object");
                            }

                            /* NewExpr newExpr = (NewExpr)*/stmt.getRightOp();

                            // We have an assignment from one local token to another.
                            Map map = (Map) localToFieldToLocal.get(stmt
                                    .getLeftOp());

                            if (map != null) {
                                doneSomething = true;

                                Local isNotNullLocal = (Local) localToIsNotNullLocal
                                        .get(stmt.getLeftOp());

                                if (debug) {
                                    System.out.println("Stmt = " + stmt);
                                }

                                body.getUnits().insertBefore(
                                        Jimple.v().newAssignStmt(
                                                isNotNullLocal,
                                                IntConstant.v(1)), unit);

                                for (Iterator tokenFields = map.keySet()
                                        .iterator(); tokenFields.hasNext();) {
                                    SootField tokenField = (SootField) tokenFields
                                            .next();
                                    Local replacementLocal = (Local) map
                                            .get(tokenField);

                                    // Initialize fields?
                                    if (debug) {
                                        System.out.println("tokenField = "
                                                + tokenField);
                                    }

                                    // FIXME: ??
                                    Value replacementValue = _getNullValueForType(replacementLocal
                                            .getType());
                                    body.getUnits().insertBefore(
                                            Jimple.v().newAssignStmt(
                                                    replacementLocal,
                                                    replacementValue), unit);
                                }

                                stmt.getRightOpBox().setValue(NullConstant.v());

                                // body.getUnits().remove(unit);
                            }
                        }

                        //  else if (stmt.getLeftOp() instanceof Local &&
                        //                                        stmt.getRightOp() instanceof InvokeExpr) {
                        //                                  System.out.println("handling as method call.");
                        //                                  // We have an assignment from one local token to another.
                        //                                  Map map = (Map)localToFieldToLocal.get(stmt.getLeftOp());
                        //                                  if (map != null) {
                        //                                      Local isNotNullLocal = (Local)
                        //                                          localToIsNotNullLocal.get(stmt.getLeftOp());
                        //                                      body.getUnits().insertAfter(
                        //                                              Jimple.v().newAssignStmt(
                        //                                                      isNotNullLocal,
                        //                                                      IntConstant.v(0)),
                        //                                              unit);
                        //                                      for (Iterator tokenFields = map.keySet().iterator();
                        //                                          tokenFields.hasNext();) {
                        //                                          SootField tokenField = (SootField)tokenFields.next();
                        //                                          Local replacementLocal = (Local)
                        //                                              map.get(tokenField);
                        //                                          // Initialize fields?
                        //                                          body.getUnits().insertAfter(
                        //                                                  Jimple.v().newAssignStmt(
                        //                                                          replacementLocal,
                        //                                                          ),
                        //                                                  unit);
                        //                                      }
                        //                                  }
                        //                                  }
                    }
                }

                // FIXME!  This does not handle null values in fields well...
                for (Iterator boxes = unit.getUseAndDefBoxes().iterator(); boxes
                        .hasNext();) {
                    ValueBox box = (ValueBox) boxes.next();
                    Value value = box.getValue();

                    if (value instanceof BinopExpr) {
                        BinopExpr expr = (BinopExpr) value;
                        boolean op1IsToken = PtolemyUtilities.isTokenType(expr
                                .getOp1().getType());
                        boolean op2IsToken = PtolemyUtilities.isTokenType(expr
                                .getOp2().getType());

                        if (op1IsToken && op2IsToken) {
                            //                             throw new RuntimeException(
                            //                                     "Unable to handle expression"
                            //                                             + " of two token types: " + unit);
                        } else if (op1IsToken
                                && expr.getOp2().getType().equals(NullType.v())) {
                            doneSomething = true;

                            Local isNotNullLocal = (Local) localToIsNotNullLocal
                                    .get(expr.getOp1());

                            if (isNotNullLocal != null) {
                                if (debug) {
                                    System.out
                                            .println("replacing binary expression "
                                                    + expr);
                                }

                                Value nullValue;

                                if (isNotNullLocal.getType().equals(
                                        BooleanType.v())) {
                                    // If the isNotNullLocal is for a regular Object.
                                    nullValue = IntConstant.v(0);
                                } else {
                                    // If the isNotNullLocal is for an Array Object.
                                    nullValue = NullConstant.v();
                                }

                                if (expr instanceof EqExpr) {
                                    box.setValue(Jimple.v().newEqExpr(
                                            isNotNullLocal, nullValue));
                                } else if (expr instanceof NeExpr) {
                                    box.setValue(Jimple.v().newNeExpr(
                                            isNotNullLocal, nullValue));
                                }
                            }
                        } else if (op2IsToken
                                && expr.getOp1().getType().equals(NullType.v())) {
                            doneSomething = true;

                            Local isNotNullLocal = (Local) localToIsNotNullLocal
                                    .get(expr.getOp2());

                            if (isNotNullLocal != null) {
                                Value nullValue;

                                if (isNotNullLocal.getType().equals(
                                        BooleanType.v())) {
                                    // If the isNotNullLocal is for a regular Object.
                                    nullValue = IntConstant.v(0);
                                } else {
                                    // If the isNotNullLocal is for an Array Object.
View Full Code Here

            //             fieldToLocal = new HashMap();
            //             localToFieldToLocal.put(baseLocal, fieldToLocal);
            return null;
        }

        Local instanceLocal = (Local) fieldToLocal.get(field);

        if (instanceLocal == null) {
            //             if (debug) System.out.println("creating new instanceLocal for " + baseLocal);
            //             // If we have not referenced this particular field
            //             // then create a new local.
View Full Code Here

        if (rightValue instanceof NullConstant) {
            if (fieldToReplacementLeftLocal != null) {
                doneSomething = true;

                Local replacementIsNotNullLocal = (Local) localToIsNotNullLocal
                        .get(leftValue);
                body.getUnits().insertBefore(
                        Jimple.v().newAssignStmt(
                                replacementIsNotNullLocal,
                                _getNullValueForType(replacementIsNotNullLocal
                                        .getType())), stmt);

                for (Iterator tokenFields = fieldToReplacementLeftLocal
                        .keySet().iterator(); tokenFields.hasNext();) {
                    SootField tokenField = (SootField) tokenFields.next();

                    if (debug) {
                        System.out.println("tokenField = " + tokenField);
                    }

                    Local replacementLocal = (Local) fieldToReplacementLeftLocal
                            .get(tokenField);

                    // FIXME: ??
                    Value replacementValue = _getNullValueForType(replacementLocal
                            .getType());
                    body.getUnits().insertBefore(
                            Jimple.v().newAssignStmt(replacementLocal,
                                    replacementValue), stmt);
                }

                //body.getUnits().remove(stmt);
            }
        } else {
            // We have an assignment from one local token to another.
            if ((fieldToReplacementLeftLocal != null)
                    && (fieldToReplacementRightLocal != null)) {
                doneSomething = true;
                body.getUnits().insertBefore(
                        Jimple.v().newAssignStmt(
                                (Local) localToIsNotNullLocal.get(leftValue),
                                (Local) localToIsNotNullLocal.get(rightValue)),
                        stmt);

                if (debug) {
                    System.out.println("local = " + leftValue);
                }

                for (Iterator tokenFields = fieldToReplacementLeftLocal
                        .keySet().iterator(); tokenFields.hasNext();) {
                    SootField tokenField = (SootField) tokenFields.next();

                    if (debug) {
                        System.out.println("tokenField = " + tokenField);
                    }

                    Local replacementLeftLocal = (Local) fieldToReplacementLeftLocal
                            .get(tokenField);
                    Local replacementRightLocal = (Local) fieldToReplacementRightLocal
                            .get(tokenField);

                    if (debug) {
                        System.out.println("replacementLeftLocal = "
                                + replacementLeftLocal);
View Full Code Here

            }

            // assume that the last statement in the body is the loop counter.
            DefinitionStmt counterStmt = (DefinitionStmt) block.getTail();

            Local counterLocal = (Local) counterStmt.getLeftOp();

            // Determine the loop increment.
            int increment = 0;

            if (counterStmt.getRightOp() instanceof AddExpr) {
View Full Code Here

                && !OverriddenMethodGenerator.isOverridden(method)) {
            Iterator locals = method.retrieveActiveBody().getLocals()
                    .iterator();

            while (locals.hasNext()) {
                Local local = (Local) locals.next();

                if (local.getType() instanceof RefType) {
                    RefType type = (RefType) local.getType();
                    classes.add(type.getSootClass());
                }
            }
        }
View Full Code Here

     */
    public void inlineBroadcast(JimpleBody body, Stmt stmt, InvokeExpr expr,
            TypedIOPort port) {
        SootClass theClass = body.getMethod().getDeclaringClass();

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

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

        Local indexArrayLocal = Jimple.v().newLocal("indexArray",
                ArrayType.v(IntType.v(), 1));
        body.getLocals().add(indexArrayLocal);

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

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

        SootField indexArrayField = (SootField) _portToIndexArrayField
                .get(port);

        if (indexArrayField == null) {
            throw new RuntimeException("indexArrayField is null!");
        }

        // Load the array of indexes.
        body.getUnits().insertBefore(
                Jimple.v().newAssignStmt(
                        indexArrayLocal,
                        Jimple.v().newInstanceFieldRef(body.getThisLocal(),
                                indexArrayField.makeRef())), stmt);

        Value bufferSizeValue = null;

        // Refer directly to the buffer in the _model
        int channel = 0;

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

            int bufferSize = _getBufferSize(relation);

            // remember the size of the buffer.
            bufferSizeValue = IntConstant.v(bufferSize);

            for (int i = 0; i < relation.getWidth(); i++, channel++) {
                Value channelValue = IntConstant.v(channel);

                // Load the correct index into indexLocal
                body.getUnits().insertBefore(
                        Jimple.v().newAssignStmt(
                                indexLocal,
                                Jimple.v().newArrayRef(indexArrayLocal,
                                        channelValue)), stmt);

                SootField arrayField = _modelClass
                        .getFieldByName(InlinePortTransformer
                                .getBufferFieldName(relation, i, port.getType()));
                Local containerLocal = FieldsForEntitiesTransformer
                        .getLocalReferenceForEntity(_model, theClass, body
                                .getThisLocal(), body, stmt, _options);

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

                // If we are calling with just a token, then send the token.
                if (expr.getArgCount() == 1) {
                    // Write to the buffer.
                    body.getUnits().insertBefore(
                            Jimple.v().newAssignStmt(
                                    Jimple.v().newArrayRef(bufferLocal,
                                            indexLocal), expr.getArg(0)), stmt);

                    // increment the position in the buffer.
                    body.getUnits().insertBefore(
                            _createIndexUpdateInstructions(indexLocal,
                                    indexArrayLocal, channelValue,
                                    bufferSizeValue), stmt);
                } else {
                    // We must send an array of tokens.
                    body.getUnits().insertBefore(
                            Jimple.v().newAssignStmt(returnArrayLocal,
                                    expr.getArg(0)), stmt);

                    // If the count is specified statically
                    if (Evaluator.isValueConstantValued(expr.getArg(1))) {
                        int argCount = ((IntConstant) Evaluator
                                .getConstantValueOf(expr.getArg(1))).value;

                        for (int k = 0; k < argCount; k++) {
                            // Get the value.
                            body.getUnits().insertBefore(
                                    Jimple.v().newAssignStmt(
                                            returnLocal,
                                            Jimple.v().newArrayRef(
                                                    returnArrayLocal,
                                                    IntConstant.v(k))), stmt);

                            // Store in the buffer array.
                            body.getUnits().insertBefore(
                                    Jimple.v().newAssignStmt(
                                            Jimple.v().newArrayRef(bufferLocal,
                                                    indexLocal), returnLocal),
                                    stmt);

                            // increment the position in the buffer.
                            body.getUnits().insertBefore(
                                    _createIndexUpdateInstructions(indexLocal,
                                            indexArrayLocal, channelValue,
                                            bufferSizeValue), stmt);
                        }
                    } else {
                        // we don't know the size beforehand,
                        // so build a loop into the code.
                        // The loop counter
                        Local counterLocal = Jimple.v().newLocal("counter",
                                IntType.v());
                        body.getLocals().add(counterLocal);

                        // The list of initializer instructions.
                        List initializerList = new LinkedList();
View Full Code Here

     */
    public void inlineGet(JimpleBody body, Stmt stmt, ValueBox box,
            InvokeExpr expr, TypedIOPort port) {
        /*SootClass theClass =*/body.getMethod().getDeclaringClass();

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

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

        Local indexArrayLocal = Jimple.v().newLocal("indexArray",
                ArrayType.v(IntType.v(), 1));
        body.getLocals().add(indexArrayLocal);

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

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

        Value channelValue = expr.getArg(0);

        Value bufferSizeValue = _getBufferAndSize(body, stmt, port, port
                .getType(), channelValue, bufferLocal,
                _portToTypeNameToBufferField, false);

        _getCorrectIndex(body, stmt, port, indexLocal, indexArrayLocal,
                channelValue, bufferSizeValue, _portToIndexArrayField);

        // If we are calling with just a channel, then read the value.
        if (expr.getArgCount() == 1) {
            body.getUnits().insertAfter(
                    _createIndexUpdateInstructions(indexLocal, indexArrayLocal,
                            channelValue, bufferSizeValue), stmt);

            // We may be calling get without setting the return value
            // to anything.
            if (stmt instanceof DefinitionStmt) {
                // Replace the get() with an array read.
                box.setValue(Jimple.v().newArrayRef(bufferLocal, indexLocal));
            } else {
                body.getUnits().remove(stmt);
            }
        } else {
            // We must return an array of tokens.
            // Create an array of the appropriate length.
            body.getUnits().insertBefore(
                    Jimple.v()
                            .newAssignStmt(
                                    returnArrayLocal,
                                    Jimple.v().newNewArrayExpr(
                                            PtolemyUtilities.tokenType,
                                            expr.getArg(1))), stmt);

            Value countValue = expr.getArg(1);

            // If the count is specified statically
            // FIXME: constant loop unroller should take care of this.
            if (Evaluator.isValueConstantValued(countValue)) {
                int argCount = ((IntConstant) Evaluator
                        .getConstantValueOf(countValue)).value;

                for (int k = 0; k < argCount; k++) {
                    // Get the value.
                    body.getUnits().insertBefore(
                            Jimple.v().newAssignStmt(
                                    returnLocal,
                                    Jimple.v().newArrayRef(bufferLocal,
                                            indexLocal)), stmt);

                    // Store in the return array.
                    body.getUnits().insertBefore(
                            Jimple.v().newAssignStmt(
                                    Jimple.v().newArrayRef(returnArrayLocal,
                                            IntConstant.v(k)), returnLocal),
                            stmt);

                    // increment the position in the buffer.
                    body.getUnits().insertBefore(
                            _createIndexUpdateInstructions(indexLocal,
                                    indexArrayLocal, channelValue,
                                    bufferSizeValue), stmt);
                }

                // Replace the get() call.
                box.setValue(returnArrayLocal);
            } else {
                // we don't know the size beforehand,
                // so build a loop into the code.
                // The loop counter
                Local counterLocal = Jimple.v()
                        .newLocal("counter", IntType.v());
                body.getLocals().add(counterLocal);

                // The list of initializer instructions.
                List initializerList = new LinkedList();
View Full Code Here

     *  given body with a circular array reference.
     */
    public void inlineGetInside(JimpleBody body, Stmt stmt, ValueBox box,
            InvokeExpr expr, TypedIOPort port) {
        /*SootClass theClass =*/body.getMethod().getDeclaringClass();
        Local bufferLocal = Jimple.v().newLocal("buffer",
                ArrayType.v(PtolemyUtilities.tokenType, 1));
        body.getLocals().add(bufferLocal);

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

        Local indexArrayLocal = Jimple.v().newLocal("indexArray",
                ArrayType.v(IntType.v(), 1));
        body.getLocals().add(indexArrayLocal);

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

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

        Value channelValue = expr.getArg(0);

        Value bufferSizeValue = _getBufferAndSize(body, stmt, port, port
                .getType(), channelValue, bufferLocal,
                _portToTypeNameToInsideBufferField, true);

        _getCorrectIndex(body, stmt, port, indexLocal, indexArrayLocal,
                channelValue, bufferSizeValue, _portToInsideIndexArrayField);

        // If we are calling with just a channel, then read the value.
        if (expr.getArgCount() == 1) {
            body.getUnits().insertAfter(
                    _createIndexUpdateInstructions(indexLocal, indexArrayLocal,
                            channelValue, bufferSizeValue), stmt);

            // We may be calling get without setting the return value
            // to anything.
            if (stmt instanceof DefinitionStmt) {
                // Replace the get() with an array read.
                box.setValue(Jimple.v().newArrayRef(bufferLocal, indexLocal));
            } else {
                body.getUnits().remove(stmt);
            }
        } else {
            // We must return an array of tokens.
            // Create an array of the appropriate length.
            body.getUnits().insertBefore(
                    Jimple.v()
                            .newAssignStmt(
                                    returnArrayLocal,
                                    Jimple.v().newNewArrayExpr(
                                            PtolemyUtilities.tokenType,
                                            expr.getArg(1))), stmt);

            Value countValue = expr.getArg(1);

            // If the count is specified statically
            // FIXME: constant loop unroller should take care of this.
            if (Evaluator.isValueConstantValued(countValue)) {
                int argCount = ((IntConstant) Evaluator
                        .getConstantValueOf(countValue)).value;

                for (int k = 0; k < argCount; k++) {
                    // Get the value.
                    body.getUnits().insertBefore(
                            Jimple.v().newAssignStmt(
                                    returnLocal,
                                    Jimple.v().newArrayRef(bufferLocal,
                                            indexLocal)), stmt);

                    // Store in the return array.
                    body.getUnits().insertBefore(
                            Jimple.v().newAssignStmt(
                                    Jimple.v().newArrayRef(returnArrayLocal,
                                            IntConstant.v(k)), returnLocal),
                            stmt);

                    // increment the position in the buffer.
                    body.getUnits().insertBefore(
                            _createIndexUpdateInstructions(indexLocal,
                                    indexArrayLocal, channelValue,
                                    bufferSizeValue), stmt);
                }

                // Replace the get() call.
                box.setValue(returnArrayLocal);
            } else {
                // we don't know the size beforehand,
                // so build a loop into the code.
                // The loop counter
                Local counterLocal = Jimple.v()
                        .newLocal("counter", IntType.v());
                body.getLocals().add(counterLocal);

                // The list of initializer instructions.
                List initializerList = new LinkedList();
View Full Code Here

     */
    public void inlineSend(JimpleBody body, Stmt stmt, InvokeExpr expr,
            TypedIOPort port) {
        /*SootClass theClass =*/body.getMethod().getDeclaringClass();

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

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

        Local indexArrayLocal = Jimple.v().newLocal("indexArray",
                ArrayType.v(IntType.v(), 1));
        body.getLocals().add(indexArrayLocal);

        // The first argument is always the channel.
        Value channelValue = expr.getArg(0);

        // We have to repeat for all of the remote types.
        Set typeSet = _getConnectedTypeList(port);

        for (Iterator types = typeSet.iterator(); types.hasNext();) {
            ptolemy.data.type.Type type = (ptolemy.data.type.Type) types.next();
            Local typeLocal = null;

            if (_forceConversions || !port.getType().equals(type)) {
                typeLocal = PtolemyUtilities.buildConstantTypeLocal(body, stmt,
                        type);
            }

            Value bufferSizeValue = _getBufferAndSize(body, stmt, port, type,
                    channelValue, bufferLocal, _portToTypeNameToBufferField,
                    false);

            _getCorrectIndex(body, stmt, port, indexLocal, indexArrayLocal,
                    channelValue, bufferSizeValue, _portToIndexArrayField);

            // A local of type token
            Local tokenLocal = Jimple.v().newLocal("tokenLocal",
                    PtolemyUtilities.tokenType);
            body.getLocals().add(tokenLocal);

            // A local of the appropriate type to store in the
            // buffer.
            Local outputTokenLocal = Jimple.v().newLocal("outputTokenLocal",
                    PtolemyUtilities.getSootTypeForTokenType(type));
            body.getLocals().add(outputTokenLocal);

            // If we are calling with just a channel, then write the value.
            if (expr.getArgCount() == 2) {
                Local sendTokenLocal = (Local) expr.getArg(1);

                // Replace the put() with an array write.
                body.getUnits().insertBefore(
                        _createBufferStoreInstructions(bufferLocal, indexLocal,
                                sendTokenLocal, typeLocal, tokenLocal,
                                outputTokenLocal), stmt);

                // increment the position in the buffer.
                body.getUnits()
                        .insertBefore(
                                _createIndexUpdateInstructions(indexLocal,
                                        indexArrayLocal, channelValue,
                                        bufferSizeValue), stmt);
            } else {
                Local sendArrayLocal = (Local) expr.getArg(1);

                /*                Jimple.v().newLocal("sendArray",
                 ArrayType.v(PtolemyUtilities.tokenType, 1));
                 body.getLocals().add(sendArrayLocal);

                 // We must send an array of tokens.
                 body.getUnits().insertBefore(
                 Jimple.v().newAssignStmt(
                 sendArrayLocal,
                 expr.getArg(1)),
                 stmt);*/
                Value countValue = expr.getArg(2);

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

                // If the count is specified statically
                if (Evaluator.isValueConstantValued(countValue)) {
                    int argCount = ((IntConstant) Evaluator
                            .getConstantValueOf(countValue)).value;

                    for (int k = 0; k < argCount; k++) {
                        // Get the value.
                        body.getUnits().insertBefore(
                                Jimple.v().newAssignStmt(
                                        sendTokenLocal,
                                        Jimple.v().newArrayRef(sendArrayLocal,
                                                IntConstant.v(k))), stmt);

                        // Store in the buffer array.
                        body.getUnits().insertBefore(
                                _createBufferStoreInstructions(bufferLocal,
                                        indexLocal, sendTokenLocal, typeLocal,
                                        tokenLocal, outputTokenLocal), stmt);

                        // increment the position in the buffer.
                        body.getUnits().insertBefore(
                                _createIndexUpdateInstructions(indexLocal,
                                        indexArrayLocal, channelValue,
                                        bufferSizeValue), stmt);
                    }
                } else {
                    // we don't know the size beforehand,
                    // so build a loop into the code.
                    // The loop counter
                    Local counterLocal = Jimple.v().newLocal("counter",
                            IntType.v());
                    body.getLocals().add(counterLocal);

                    // The list of initializer instructions.
                    List initializerList = new LinkedList();
View Full Code Here

TOP

Related Classes of soot.Local

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.