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 29, 1998 by Raja Vallee-Rai (kor@sable.mcgill.ca) (*) 00106 Corrected the ints2long method. 00107 00108 - Modified on 15-Jun-1998 by Raja Vallee-Rai (kor@sable.mcgill.ca). (*) 00109 First internal release (Version 0.1). 00110 */ 00111 00112 import java.io.*; 00113 import java.util.StringTokenizer; 00114 import java.util.NoSuchElementException; 00115 00116 /** Base abstract class for constant pool entries; includes some utility methods. 00117 * @see ClassFile#constant_pool 00118 * @author Clark Verbrugge 00119 */ 00120 abstract class cp_info { 00121 00122 public static final byte CONSTANT_Utf8 = 1; 00123 public static final byte CONSTANT_Integer = 3; 00124 public static final byte CONSTANT_Float = 4; 00125 public static final byte CONSTANT_Long = 5; 00126 public static final byte CONSTANT_Double = 6; 00127 public static final byte CONSTANT_Class = 7; 00128 public static final byte CONSTANT_String = 8; 00129 public static final byte CONSTANT_Fieldref = 9; 00130 public static final byte CONSTANT_Methodref = 10; 00131 public static final byte CONSTANT_InterfaceMethodref = 11; 00132 public static final byte CONSTANT_NameAndType = 12; 00133 00134 /** One of the CONSTANT_* constants. */ 00135 public byte tag; 00136 00137 /** Compares this entry with another cp_info object (which may reside 00138 * in a different constant pool). 00139 * @param constant_pool constant pool of ClassFile for this. 00140 * @param cp constant pool entry to compare against. 00141 * @param cp_constant_pool constant pool of ClassFile for cp. 00142 * @return a value <0, 0, or >0 indicating whether this is smaller, 00143 * the same or larger than cp. 00144 */ 00145 public abstract int compareTo(cp_info constant_pool[],cp_info cp, 00146 cp_info cp_constant_pool[]); 00147 /** Counts the number of parameters of the given method. 00148 * @param constant_pool constant pool of ClassFile. 00149 * @param m a constant pool index as accepted by getTypeDescr. 00150 * @return the number of parameters. 00151 * @see ClassFile#parseMethodDesc_params 00152 * @see cp_info#getTypeDescr 00153 */ 00154 public static int countParams(cp_info constant_pool[],int m) { 00155 StringTokenizer st; 00156 String s = getTypeDescr(constant_pool,m); 00157 s = ClassFile.parseMethodDesc_params(s); 00158 st = new StringTokenizer(s,",",false); 00159 return st.countTokens(); 00160 } 00161 /** Returns the name of the field type of the given constant pool object. 00162 * @param constant_pool constant pool of ClassFile. 00163 * @param i a constant pool index for an entry of type CONSTANT_Utf8, 00164 * CONSTANT_NameAndType, or CONSTANT_FieldRef. 00165 * @return the type of the field. 00166 * @see CONSTANT_Utf8_info 00167 * @see CONSTANT_NameAndType_info#name_index 00168 * @see CONSTANT_Fieldref_info#name_and_type_index 00169 * @see cp_info#getTypeDescr 00170 * @see ClassFile#parseDesc 00171 */ 00172 public static String fieldType(cp_info constant_pool[],int i) { 00173 return ClassFile.parseDesc(getTypeDescr(constant_pool,i),""); 00174 } 00175 /** Locates the name of the corresponding class, given the constant 00176 * pool index of either a CONSTANT_Class, _Fieldref, Methodref or 00177 * InterfaceMethodref. 00178 * @param constant_pool constant pool of ClassFile. 00179 * @param i index of cp_info entry in question. 00180 * @return name of the associated class. 00181 * @see CONSTANT_Class_info#toString(constant_pool) 00182 * @see CONSTANT_Fieldref_info#class_index 00183 * @see CONSTANT_Methodref_info#class_index 00184 * @see CONSTANT_InterfaceMethodref_info#class_index 00185 */ 00186 public static String getClassname(cp_info constant_pool[],int i) { 00187 cp_info c = constant_pool[i]; 00188 switch (c.tag) { 00189 case cp_info.CONSTANT_Class: 00190 return c.toString(constant_pool); 00191 case cp_info.CONSTANT_Fieldref: 00192 return getClassname(constant_pool,((CONSTANT_Fieldref_info)c).class_index); 00193 case cp_info.CONSTANT_Methodref: 00194 return getClassname(constant_pool,((CONSTANT_Methodref_info)c).class_index); 00195 case cp_info.CONSTANT_InterfaceMethodref: 00196 return getClassname(constant_pool, 00197 ((CONSTANT_InterfaceMethodref_info)c).class_index); 00198 } 00199 System.out.println("Request for classname for non-class object!"); 00200 return "Can't find classname. Sorry."; 00201 } 00202 /** Returns the name of the given constant pool object, assuming it is 00203 * of type CONSTANT_NameAndType, _FieldRef, _Methodref or _InterfaceMethodref. 00204 * @param constant_pool constant pool of ClassFile. 00205 * @param i index of cp_info entry in question. 00206 * @return name of the associated object. 00207 * @see CONSTANT_Utf8_info 00208 * @see CONSTANT_NameAndType_info#name_index 00209 * @see CONSTANT_Fieldref_info#name_and_type_index 00210 * @see CONSTANT_Methodref_info#name_and_type_index 00211 * @see CONSTANT_InterfaceMethodref_info#name_and_type_index 00212 */ 00213 public static String getName(cp_info constant_pool[],int i) { 00214 cp_info c = constant_pool[i]; 00215 switch (c.tag) { 00216 case cp_info.CONSTANT_Utf8: 00217 return c.toString(constant_pool); 00218 case cp_info.CONSTANT_NameAndType: 00219 return getName(constant_pool,((CONSTANT_NameAndType_info)c).name_index); 00220 case cp_info.CONSTANT_Fieldref: 00221 return getName(constant_pool,((CONSTANT_Fieldref_info)c).name_and_type_index); 00222 case cp_info.CONSTANT_Methodref: 00223 return getName(constant_pool,((CONSTANT_Methodref_info)c).name_and_type_index); 00224 case cp_info.CONSTANT_InterfaceMethodref: 00225 return getName(constant_pool, 00226 ((CONSTANT_InterfaceMethodref_info)c).name_and_type_index); 00227 } 00228 System.out.println("Request for name for non-named object!"); 00229 return "Can't find name of that object. Sorry."; 00230 } 00231 /** Returns the type descriptor for the given constant pool 00232 * object, which must be a CONSTANT_Utf8, CONSTANT_NameAndType, 00233 * CONSTANT_Fieldref, CONSTANT_MethodRef, or CONSTANT_InterfaceMethodRef. 00234 * @param constant_pool constant pool of ClassFile. 00235 * @param i a constant pool index for an entry of type CONSTANT_Utf8, 00236 * CONSTANT_NameAndType, CONSTANT_MethodRef, or CONSTANT_InterfaceMethodRef. 00237 * @return the type descriptor. 00238 * @see CONSTANT_Utf8_info 00239 * @see CONSTANT_NameAndType_info#name_index 00240 * @see CONSTANT_Fieldref_info#name_and_type_index 00241 * @see CONSTANT_Methodref_info#name_and_type_index 00242 * @see CONSTANT_InterfaceMethodref_info#name_and_type_index 00243 */ 00244 public static String getTypeDescr(cp_info constant_pool[],int i) { 00245 cp_info c = constant_pool[i]; 00246 if (c instanceof CONSTANT_Utf8_info) 00247 return c.toString(constant_pool); 00248 if (c instanceof CONSTANT_NameAndType_info) 00249 return getTypeDescr(constant_pool, 00250 ((CONSTANT_NameAndType_info)c).descriptor_index); 00251 if (c instanceof CONSTANT_Methodref_info) 00252 return getTypeDescr(constant_pool, 00253 ((CONSTANT_Methodref_info)c).name_and_type_index); 00254 if (c instanceof CONSTANT_InterfaceMethodref_info) 00255 return getTypeDescr(constant_pool, 00256 ((CONSTANT_InterfaceMethodref_info)c).name_and_type_index); 00257 if (c instanceof CONSTANT_Fieldref_info) 00258 return getTypeDescr(constant_pool, 00259 ((CONSTANT_Fieldref_info)c).name_and_type_index); 00260 System.out.println("Invalid request for type descr!"); 00261 return "Invalid type descriptor request."; 00262 } 00263 /** Utility method, converts two integers into a single long. 00264 * @param high upper 32 bits of the long. 00265 * @param low lower 32 bits of the long. 00266 * @return a long value composed from the two ints. 00267 */ 00268 public static long ints2long(long high,long low) { 00269 long h,l; 00270 h = high; l = low; 00271 return ((h<<32) + l); 00272 } 00273 /** Utility method, returns a String binary representation of the 00274 * given integer. 00275 * @param i the integer in question. 00276 * @return a String of 0's and 1's. 00277 * @see cp_info#printBits(long) 00278 */ 00279 public static String printBits(int i) { 00280 String s = ""; 00281 int j,k; 00282 k = 1; 00283 for (j=0;j<32; j++) { 00284 if ((i&k)!=0) 00285 s = "1" + s; 00286 else 00287 s = "0" + s; 00288 k = k<<1; 00289 } 00290 return s; 00291 } 00292 /** Utility method, returns a String binary representation of the 00293 * given long. 00294 * @param i the long in question. 00295 * @return a String of 0's and 1's. 00296 * @see cp_info#printBits(int) 00297 */ 00298 public static String printBits(long i) { 00299 String s = ""; 00300 long j,k; 00301 k = 1; 00302 for (j=0;j<64; j++) { 00303 if ((i&k)!=0) 00304 s = "1" + s; 00305 else 00306 s = "0" + s; 00307 k = k<<1; 00308 } 00309 return s; 00310 } 00311 /** Returns the size of this entry. 00312 * @return size (in bytes) of this entry. 00313 */ 00314 public abstract int size(); 00315 /** Returns a String representation of this entry. 00316 * @param constant_pool constant pool of ClassFile. 00317 * @return String representation of this entry. 00318 */ 00319 public abstract String toString(cp_info constant_pool[]); 00320 /** Returns a String description of what kind of entry this is. 00321 * @return String representation of this kind of entry. 00322 */ 00323 public abstract String typeName(); 00324 }