Package soot

Examples of soot.SootClass


        String name = CNames.typeNameOf(v.getType());

        // Put a #include for the appropriate type.
        if (type instanceof RefType) {
            SootClass sootClass = ((RefType) type).getSootClass();
            _context.addIncludeFile("\"" + CNames.includeFileNameOf(sootClass)
                    + "\"");
        }

        _push("( malloc(sizeof(struct " + name + ")))");
View Full Code Here


        SootMethod method = v.getMethod();

        // Generate cast for first argument of method.
        String cast = "";

        SootClass declaringClass = method.getDeclaringClass();

        if (!declaringClass.isInterface()) {
            cast = "(" + CNames.instanceNameOf(declaringClass)
                    + "/* actual cast */)";
        }

        Iterator inheritedMethods = MethodListGenerator.getInheritedMethods(
                declaringClass).iterator();

        while (inheritedMethods.hasNext()) {
            SootMethod inheritedMethod = (SootMethod) inheritedMethods.next();

            if (inheritedMethod.getSubSignature().equals(
                    method.getSubSignature())) {
                cast = "("
                        + CNames.instanceNameOf(inheritedMethod
                                .getDeclaringClass()) + "/* inherited cast */)";
                break;
            }
        }

        if (!(v.getBase().getType() instanceof RefType)) {
            _unexpectedCase(v, "RefType base type expected.");
        } else {
            SootClass baseClass = ((RefType) (v.getBase().getType()))
                    .getSootClass();

            if (baseClass == declaringClass) {
                if (method.isStatic()) {
                    _unexpectedCase(v, "Non-static method expected.");
                } else {
                    _generateInstanceInvokeExpression(v);
                }
            } else if ((!baseClass.hasSuperclass())
                    || (baseClass.getSuperclass() != declaringClass)) {
                _unexpectedCase(v,
                        "Expected method class to be superclass of base");

                // If we are generating code in single class mode, then
                // we are not supporting inheritance, so ignore invocation
View Full Code Here

                String fieldName = ModelTransformer.getFieldNameForEntity(
                        entity, model);
                SootField field = modelClass.getFieldByName(fieldName);
                String className = ModelTransformer.getInstanceClassName(
                        entity, options);
                SootClass theClass = Scene.v().loadClassAndSupport(className);
                SootMethod preinitializeMethod = SootUtilities
                        .searchForMethodByName(theClass, "preinitialize");
                Local actorLocal = Jimple.v().newLocal("actor",
                        RefType.v(theClass));
                body.getLocals().add(actorLocal);

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

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

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

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

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

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

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

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

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

            Local thisLocal = body.getThisLocal();

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

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

            String fieldName = ModelTransformer.getFieldNameForEntity(
                    controller, model);
            SootField field = modelClass.getFieldByName(fieldName);
            String className = ModelTransformer.getInstanceClassName(
                    controller, options);
            SootClass theClass = Scene.v().loadClassAndSupport(className);
            SootMethod actorPrefireMethod = SootUtilities
                    .searchForMethodByName(theClass, "prefire");

            units.insertBefore(
                    Jimple.v().newAssignStmt(
                            actorLocal,
                            Jimple.v().newInstanceFieldRef(thisLocal,
                                    field.makeRef())), insertPoint);
            units.insertBefore(Jimple.v().newAssignStmt(
                    prefireReturnsLocal,
                    Jimple.v().newVirtualInvokeExpr(actorLocal,
                            actorPrefireMethod.makeRef())), insertPoint);

            units.insertBefore(Jimple.v().newReturnStmt(prefireReturnsLocal),
                    insertPoint);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            {
                // Fire the controller.
                Local actorLocal = Jimple.v().newLocal("actor", actorType);
                body.getLocals().add(actorLocal);

                String fieldName = ModelTransformer.getFieldNameForEntity(
                        controller, model);
                SootField field = modelClass.getFieldByName(fieldName);
                String className = ModelTransformer.getInstanceClassName(
                        controller, options);
                SootClass theClass = Scene.v().loadClassAndSupport(className);
                SootMethod actorFireMethod = SootUtilities
                        .searchForMethodByName(theClass, "fire");

                units.insertBefore(Jimple.v().newAssignStmt(
                        actorLocal,
                        Jimple.v().newInstanceFieldRef(thisLocal,
                                field.makeRef())), insertPoint);
                units.insertBefore(Jimple.v().newInvokeStmt(
                        Jimple.v().newVirtualInvokeExpr(actorLocal,
                                actorFireMethod.makeRef())), insertPoint);
            }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            String fieldName = ModelTransformer.getFieldNameForEntity(
                    controller, model);
            SootField field = modelClass.getFieldByName(fieldName);
            String className = ModelTransformer.getInstanceClassName(
                    controller, options);
            SootClass theClass = Scene.v().loadClassAndSupport(className);
            SootMethod actorPostfireMethod = SootUtilities
                    .searchForMethodByName(theClass, "postfire");

            units.insertBefore(
                    Jimple.v().newAssignStmt(
                            actorLocal,
                            Jimple.v().newInstanceFieldRef(thisLocal,
                                    field.makeRef())), insertPoint);
            units.insertBefore(Jimple.v().newAssignStmt(
                    postfireReturnsLocal,
                    Jimple.v().newVirtualInvokeExpr(actorLocal,
                            actorPostfireMethod.makeRef())), insertPoint);

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

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

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

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

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

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

            //                             tokenArrayLocal)),
            //                     insertPoint);
            //             return tokenLocal;
        } else if (token.getClass().equals(Token.class)) {
            // Token has no string constructor.
            SootClass tokenClass = Scene.v().loadClassAndSupport(
                    token.getClass().getName());
            SootMethod tokenConstructor = tokenClass.getMethod("void <init>()");

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

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

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

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

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

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

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

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

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

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

            // If we have a native type, then ignore because it can't
            // be a token type.
            return null;
        }

        SootClass objectClass = returnType.getSootClass();

        if (SootUtilities.derivesFrom(objectClass, PtolemyUtilities.tokenClass)
                || objectClass.getName().equals(
                        "ptolemy.data.BitwiseOperationToken")) {
            return returnType;
        }

        return null;
View Full Code Here

            // If we have a native type, then ignore because it can't
            // be a token type.
            return null;
        }

        SootClass objectClass = returnType.getSootClass();

        if (SootUtilities.derivesFrom(objectClass, PtolemyUtilities.typeClass)) {
            return returnType;
        }
View Full Code Here

            }
        } else {
            return false;
        }

        SootClass tokenClass = refType.getSootClass();

        if (tokenClass.equals(PtolemyUtilities.tokenClass)
                || tokenClass.equals(PtolemyUtilities.scalarTokenClass)) {
            return false;
        }

        return SootUtilities.derivesFrom(refType.getSootClass(),
                PtolemyUtilities.tokenClass);
View Full Code Here

        // Call the Class Structure Initializations for all required
        // classes.
        headerCode.append("\n");

        while (requiredClasses.hasNext()) {
            SootClass nextClass = (SootClass) requiredClasses.next();
            headerCode.append("#include \""
                    + CNames.includeFileNameOf(nextClass) + "\"\n");
        }

        headerCode.append("\n");
View Full Code Here

    /** Copy a class */
    public static SootClass copyClass(SootClass oldClass, String newClassName) {
        //System.out.println("SootClass.copyClass(" + oldClass + ", "
        //                   + newClassName + ")");
        // Create the new Class
        SootClass newClass = new SootClass(newClassName, oldClass
                .getModifiers());

        try {
            Scene.v().addClass(newClass);
        } catch (RuntimeException runtime) {
            throw new RuntimeException("Perhaps you are calling the same "
                    + "transform twice?: " + runtime);
        }

        // Set the Superclass.
        newClass.setSuperclass(oldClass.getSuperclass());

        // Copy the interface declarations
        newClass.getInterfaces().addAll(oldClass.getInterfaces());

        // Copy the fields.
        _copyFields(newClass, oldClass);

        // Copy the methods.
        Iterator methods = oldClass.getMethods().iterator();

        while (methods.hasNext()) {
            SootMethod oldMethod = (SootMethod) methods.next();

            SootMethod newMethod = new SootMethod(oldMethod.getName(),
                    oldMethod.getParameterTypes(), oldMethod.getReturnType(),
                    oldMethod.getModifiers(), oldMethod.getExceptions());
            newClass.addMethod(newMethod);

            JimpleBody body = Jimple.v().newBody(newMethod);
            body.importBodyContentsFrom(oldMethod.retrieveActiveBody());
            newMethod.setActiveBody(body);
        }
View Full Code Here

TOP

Related Classes of soot.SootClass

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.