00001 package ca.mcgill.sable.soot.coffi; 00002 00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00004 * Jimple, a 3-address code Java(TM) bytecode representation. * 00005 * Copyright (C) 1997, 1998 Raja Vallee-Rai (kor@sable.mcgill.ca) * 00006 * All rights reserved. * 00007 * * 00008 * This work was done as a project of the Sable Research Group, * 00009 * School of Computer Science, McGill University, Canada * 00010 * (http://www.sable.mcgill.ca/). It is understood that any * 00011 * modification not identified as such is not covered by the * 00012 * preceding statement. * 00013 * * 00014 * This work is free software; you can redistribute it and/or * 00015 * modify it under the terms of the GNU Library General Public * 00016 * License as published by the Free Software Foundation; either * 00017 * version 2 of the License, or (at your option) any later version. * 00018 * * 00019 * This work is distributed in the hope that it will be useful, * 00020 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00021 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00022 * Library General Public License for more details. * 00023 * * 00024 * You should have received a copy of the GNU Library General Public * 00025 * License along with this library; if not, write to the * 00026 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * 00027 * Boston, MA 02111-1307, USA. * 00028 * * 00029 * Java is a trademark of Sun Microsystems, Inc. * 00030 * * 00031 * To submit a bug report, send a comment, or get the latest news on * 00032 * this project and other Sable Research Group projects, please * 00033 * visit the web site: http://www.sable.mcgill.ca/ * 00034 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00035 00036 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00037 * Coffi, a bytecode parser for the Java(TM) language. * 00038 * Copyright (C) 1996, 1997 Clark Verbrugge (clump@sable.mcgill.ca). * 00039 * All rights reserved. * 00040 * * 00041 * This work was done as a project of the Sable Research Group, * 00042 * School of Computer Science, McGill University, Canada * 00043 * (http://www.sable.mcgill.ca/). It is understood that any * 00044 * modification not identified as such is not covered by the * 00045 * preceding statement. * 00046 * * 00047 * This work is free software; you can redistribute it and/or * 00048 * modify it under the terms of the GNU Library General Public * 00049 * License as published by the Free Software Foundation; either * 00050 * version 2 of the License, or (at your option) any later version. * 00051 * * 00052 * This work is distributed in the hope that it will be useful, * 00053 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00054 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00055 * Library General Public License for more details. * 00056 * * 00057 * You should have received a copy of the GNU Library General Public * 00058 * License along with this library; if not, write to the * 00059 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * 00060 * Boston, MA 02111-1307, USA. * 00061 * * 00062 * Java is a trademark of Sun Microsystems, Inc. * 00063 * * 00064 * To submit a bug report, send a comment, or get the latest news on * 00065 * this project and other Sable Research Group projects, please * 00066 * visit the web site: http://www.sable.mcgill.ca/ * 00067 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00068 00069 /* 00070 Reference Version 00071 ----------------- 00072 This is the latest official version on which this file is based. 00073 The reference version is: $CoffiVersion: 1.1 $ 00074 $SootVersion: 1.beta.4 $ 00075 00076 Change History 00077 -------------- 00078 A) Notes: 00079 00080 Please use the following template. Most recent changes should 00081 appear at the top of the list. 00082 00083 - Modified on [date (March 1, 1900)] by [name]. [(*) if appropriate] 00084 [description of modification]. 00085 00086 Any Modification flagged with "(*)" was done as a project of the 00087 Sable Research Group, School of Computer Science, 00088 McGill University, Canada (http://www.sable.mcgill.ca/). 00089 00090 You should add your copyright, using the following template, at 00091 the top of this file, along with other copyrights. 00092 00093 * * 00094 * Modifications by [name] are * 00095 * Copyright (C) [year(s)] [your name (or company)]. All rights * 00096 * reserved. * 00097 * * 00098 00099 B) Changes: 00100 00101 - Modified on November 2, 1998 by Raja Vallee-Rai (kor@sable.mcgill.ca) (*) 00102 Repackaged all source files and performed extensive modifications. 00103 First initial release of Soot. 00104 00105 - Modified on September 3, 1998 by Raja Vallee-Rai (kor@sable.mcgill.ca). (*) 00106 Improved the error message on merge failure. 00107 00108 - Modified on July 23, 1998 by Raja Vallee-Rai (kor@sable.mcgill.ca). (*) 00109 Added a clone method. 00110 00111 - Modified on 15-Jun-1998 by Raja Vallee-Rai (kor@sable.mcgill.ca). (*) 00112 First internal release (Version 0.1). 00113 */ 00114 00115 import java.io.*; 00116 00117 import ca.mcgill.sable.soot.*; 00118 00119 /* 00120 * A less resource hungry implementation of the TypeStack would just have pointers to 00121 * 'sub-stacks' instead of copying the entire array around. 00122 */ 00123 00124 class TypeStack implements ca.mcgill.sable.util.ValueObject 00125 { 00126 private static SootClassManager cm; 00127 00128 private Type[] types; 00129 00130 private TypeStack() 00131 { 00132 // no constructor 00133 } 00134 public Object clone() 00135 { 00136 TypeStack newTypeStack = new TypeStack(); 00137 00138 newTypeStack.cm = this.cm; 00139 newTypeStack.types = (Type[]) types.clone(); 00140 00141 return newTypeStack; 00142 } 00143 public boolean equals(Object object) 00144 { 00145 if(object instanceof TypeStack) 00146 { 00147 TypeStack otherStack = (TypeStack) object; 00148 00149 if(otherStack.types.length != types.length) 00150 return false; 00151 00152 for(int i = 0; i < types.length; i++) 00153 if(!types[i].equals(otherStack.types[i])) 00154 return false; 00155 00156 return true; 00157 } 00158 else 00159 return false; 00160 } 00161 public Type get(int index) 00162 { 00163 return types[index]; 00164 } 00165 public TypeStack merge(TypeStack other) 00166 { 00167 00168 if(types.length != other.types.length) 00169 throw new RuntimeException("TypeStack merging failed; unequal " + 00170 "stack lengths: " + types.length + " and " + other.types.length); 00171 00172 TypeStack newStack = new TypeStack(); 00173 00174 newStack.types = new Type[other.types.length]; 00175 00176 for(int i = 0; i < types.length; i++) 00177 if(types[i].equals(other.types[i])) 00178 newStack.types[i] = types[i]; 00179 else { 00180 if(!(types[i] instanceof RefType) || !(other.types[i] instanceof 00181 RefType)) 00182 { 00183 throw new RuntimeException("TypeStack merging failed; incompatible types " + types[i] + " and " + other.types[i]); 00184 } 00185 00186 // System.out.println("Merging: " + types[i] + " with " + other.types[i]); 00187 00188 newStack.types[i] = RefType.v("java.lang.Object"); 00189 } 00190 00191 return newStack; 00192 } 00193 public TypeStack pop() 00194 { 00195 TypeStack newStack = new TypeStack(); 00196 00197 newStack.types = new Type[types.length - 1]; 00198 System.arraycopy(types, 0, newStack.types, 0, types.length - 1); 00199 00200 return newStack; 00201 } 00202 public void print(PrintStream out) 00203 { 00204 for(int i = types.length - 1; i >= 0; i--) 00205 out.println(i + ": " + types[i].toString()); 00206 00207 if(types.length == 0) 00208 out.println("<empty>"); 00209 } 00210 public TypeStack push(Type type) 00211 { 00212 TypeStack newStack = new TypeStack(); 00213 00214 newStack.types = new Type[types.length + 1]; 00215 System.arraycopy(types, 0, newStack.types, 0, types.length); 00216 00217 newStack.types[types.length] = type; 00218 00219 return newStack; 00220 } 00221 public static void setClassManager(SootClassManager cm) 00222 { 00223 TypeStack.cm = cm; 00224 } 00225 public Type top() 00226 { 00227 if(types.length == 0) 00228 throw new RuntimeException("TypeStack is empty"); 00229 else 00230 return types[types.length - 1]; 00231 } 00232 public int topIndex() 00233 { 00234 return types.length - 1; 00235 } 00236 /** 00237 * Returns an empty stack. 00238 */ 00239 00240 public static TypeStack v() 00241 { 00242 TypeStack typeStack = new TypeStack(); 00243 00244 typeStack.types = new Type[0]; 00245 00246 return typeStack; 00247 } 00248 }