Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

PredicateInterpreter.java

00001 package edu.ksu.cis.bandera.abstraction;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 1998-2001 SAnToS Laboratories (santos@cis.ksu.edu)  *
00006 
00007  * All rights reserved.                                              *
00008  *                                                                   *
00009  * This work was done as a project in the SAnToS Laboratory,         *
00010  * Department of Computing and Information Sciences, Kansas State    *
00011  * University, USA (http://www.cis.ksu.edu/santos).                  *
00012  * It is understood that any modification not identified as such is  *
00013  * not covered by the preceding statement.                           *
00014  *                                                                   *
00015  * This work is free software; you can redistribute it and/or        *
00016  * modify it under the terms of the GNU Library General Public       *
00017  * License as published by the Free Software Foundation; either      *
00018  * version 2 of the License, or (at your option) any later version.  *
00019  *                                                                   *
00020  * This work is distributed in the hope that it will be useful,      *
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00023  * Library General Public License for more details.                  *
00024  *                                                                   *
00025  * You should have received a copy of the GNU Library General Public *
00026  * License along with this toolkit; if not, write to the             *
00027  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00028  * Boston, MA  02111-1307, USA.                                      *
00029  *                                                                   *
00030  * Java is a trademark of Sun Microsystems, Inc.                     *
00031  *                                                                   *
00032  * To submit a bug report, send a comment, or get the latest news on *
00033  * this project and other SAnToS projects, please visit the web-site *
00034  *                http://www.cis.ksu.edu/santos                      *
00035  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00036 import ca.mcgill.sable.soot.grimp.*;
00037 import ca.mcgill.sable.soot.jimple.*;
00038 import edu.ksu.cis.bandera.jext.*;
00039 import edu.ksu.cis.bandera.abstraction.typeinference.*;
00040 import edu.ksu.cis.bandera.abstraction.util.*;
00041 import java.util.*;
00042 public class PredicateInterpreter extends AbstractBanderaValueSwitch {
00043     private TypeTable typeTable;
00044     private Vector variables;
00045     private int[] tokens;
00046     private int result;
00047 public void caseAddExpr(AddExpr v) {
00048     doOp(v, "add", false);
00049 }
00050 public void caseAndExpr(AndExpr v) {
00051     doOp(v, "and", false);
00052 }
00053 public void caseArrayRef(ArrayRef v) {
00054     result = tokens[variables.indexOf(v)];
00055 }
00056 public void caseDivExpr(DivExpr v) {
00057     doOp(v, "div", false);
00058 }
00059 public void caseDoubleConstant(DoubleConstant v) {
00060     String aName = typeTable.get(v).getClass().getName();
00061     result = 1 << ((Integer) AbstractionClassLoader.invokeMethod(aName, "abs", new Class[] {double.class}, null, new Object[] {new Double(v.value)})).intValue();
00062 }
00063 public void caseEqExpr(EqExpr v) {
00064     doOp(v, "eq", true);
00065 }
00066 public void caseFloatConstant(FloatConstant v) {
00067     String aName = typeTable.get(v).getClass().getName();
00068     result = 1 << ((Integer) AbstractionClassLoader.invokeMethod(aName, "abs", new Class[] {double.class}, null, new Object[] {new Double(v.value)})).intValue();
00069 }
00070 public void caseGeExpr(GeExpr v) {
00071     doOp(v, "ge", true);
00072 }
00073 public void caseGtExpr(GtExpr v) {
00074     doOp(v, "gt", true);
00075 }
00076 public void caseInstanceFieldRef(InstanceFieldRef v) {
00077     result = tokens[variables.indexOf(v)];
00078 }
00079 public void caseIntConstant(IntConstant v) {
00080     Abstraction a = typeTable.get(v);
00081     String aName = a.getClass().getName();
00082     if (a instanceof RealAbstraction) {
00083         result = 1 << ((Integer) AbstractionClassLoader.invokeMethod(aName, "abs", new Class[] {double.class}, null, new Object[] {new Double(v.value)})).intValue();
00084     } else if (a instanceof IntegralAbstraction) {
00085         result = 1 << ((Integer) AbstractionClassLoader.invokeMethod(aName, "abs", new Class[] {long.class}, null, new Object[] {new Long(v.value)})).intValue();
00086     } else {
00087         throw new RuntimeException("Error in predicate interpreter");
00088     }
00089 }
00090 public void caseLeExpr(LeExpr v) {
00091     doOp(v, "le", true);
00092 }
00093 public void caseLengthExpr(LengthExpr v) {
00094     result = tokens[variables.indexOf(v)];
00095 }
00096 /**
00097  * 
00098  * @param v edu.ksu.cis.bandera.jext.LocalExpr
00099  */
00100 public void caseLocalExpr(LocalExpr v) {
00101     result = tokens[variables.indexOf(v)];
00102 }
00103 public void caseLongConstant(LongConstant v) {
00104     Abstraction a = typeTable.get(v);
00105     String aName = a.getClass().getName();
00106     if (a instanceof RealAbstraction) {
00107         result = 1 << ((Integer) AbstractionClassLoader.invokeMethod(aName, "abs", new Class[] {double.class}, null, new Object[] {new Double(v.value)})).intValue();
00108     } else if (a instanceof IntegralAbstraction) {
00109         result = 1 << ((Integer) AbstractionClassLoader.invokeMethod(aName, "abs", new Class[] {long.class}, null, new Object[] {new Long(v.value)})).intValue();
00110     } else {
00111         throw new RuntimeException("Error in predicate interpreter");
00112     }
00113 }
00114 public void caseLtExpr(LtExpr v) {
00115     doOp(v, "lt", true);
00116 }
00117 public void caseMulExpr(MulExpr v) {
00118     doOp(v, "mul", false);
00119 }
00120 public void caseNeExpr(NeExpr v) {
00121     doOp(v, "ne", true);
00122 }
00123 public void caseNegExpr(NegExpr v) {
00124 }
00125 public void caseOrExpr(OrExpr v) {
00126     doOp(v, "or", false);
00127 }
00128 public void caseRemExpr(RemExpr v) {
00129     doOp(v, "rem", false);
00130 }
00131 public void caseShlExpr(ShlExpr v) {
00132     doOp(v, "shl", false);
00133 }
00134 public void caseShrExpr(ShrExpr v) {
00135     doOp(v, "shr", false);
00136 }
00137 public void caseStaticFieldRef(StaticFieldRef v) {
00138     result = tokens[variables.indexOf(v)];
00139 }
00140 public void caseSubExpr(SubExpr v) {
00141     doOp(v, "sub", false);
00142 }
00143 public void caseUshrExpr(UshrExpr v) {
00144     doOp(v, "ushr", false);
00145 }
00146 public void caseXorExpr(XorExpr v) {
00147     doOp(v, "xor", false);
00148 }
00149 /**
00150  * 
00151  * @param o java.lang.Object
00152  */
00153 public void defaultCase(Object o) {
00154     throw new RuntimeException(o + " is not handled by the predicate interpreter");
00155 }
00156 /**
00157  * 
00158  * @param v ca.mcgill.sable.soot.jimple.BinopExpr
00159  * @param methodName java.lang.String
00160  */
00161 private void doOp(BinopExpr v, String methodName, boolean isTest) {
00162     Abstraction a = typeTable.get(v);
00163     v.getOp1().apply(this);
00164     int v1 = result;
00165     v.getOp2().apply(this);
00166     int v2 = result;
00167     try {
00168         if (isTest) {
00169             result = ((Byte) AbstractionClassLoader.invokeMethod(a.getClass().getName(), methodName + "Set", new Class[] {int.class, int.class}, null, new Object[] {new Integer(v1), new Integer(v2)})).intValue();
00170         } else {
00171             result = ((Integer) AbstractionClassLoader.invokeMethod(a.getClass().getName(), methodName + "Set", new Class[] {int.class, int.class}, null, new Object[] {new Integer(v1), new Integer(v2)})).intValue();
00172         }
00173         if (result == -1 || (!isTest && (result == 0))) {
00174             throw new Exception(v + " give result an invalid token set");
00175         }
00176     } catch (Exception e) {
00177         throw new RuntimeException(e.getMessage());
00178     }
00179 }
00180 /**
00181  * 
00182  * @return boolean
00183  * @param typeTable edu.ksu.cis.bandera.abstraction.typeinference.TypeTable
00184  * @param variables java.util.Vector
00185  * @param tokens int[]
00186  * @param v ca.mcgill.sable.soot.jimple.Value
00187  */
00188 public boolean isFalse(TypeTable typeTable, Vector variables, int[] tokens, Value v) {
00189     this.typeTable = typeTable;
00190     this.variables = variables;
00191     this.tokens = tokens;
00192     v.apply(this);
00193     return result == Abstraction.FALSE;
00194 }
00195 /**
00196  * 
00197  * @return boolean
00198  * @param typeTable edu.ksu.cis.bandera.abstraction.typeinference.TypeTable
00199  * @param variables java.util.Vector
00200  * @param tokens int[]
00201  * @param v ca.mcgill.sable.soot.jimple.Value
00202  */
00203 public boolean isTop(TypeTable typeTable, Vector variables, int[] tokens, Value v) {
00204     this.typeTable = typeTable;
00205     this.variables = variables;
00206     this.tokens = tokens;
00207     v.apply(this);
00208     return result == Abstraction.TRUE_OR_FALSE;
00209 }
00210 /**
00211  * 
00212  * @return boolean
00213  * @param v ca.mcgill.sable.soot.jimple.Value
00214  */
00215 public boolean isTrue(TypeTable typeTable, Vector variables, int[] tokens, Value v) {
00216     this.typeTable = typeTable;
00217     this.variables = variables;
00218     this.tokens = tokens;
00219     v.apply(this);
00220     return result == Abstraction.TRUE;
00221 }
00222 }

Generated at Thu Feb 7 06:52:28 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001