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 15-Jun-1998 by Raja Vallee-Rai (kor@sable.mcgill.ca). (*) 00106 First internal release (Version 0.1). 00107 */ 00108 00109 import java.io.*; 00110 /** Instruction subclasses are used to represent parsed bytecode; each 00111 * bytecode operation has a corresponding subclass of Instruction. 00112 * <p> 00113 * Each subclass is derived from one of 00114 * <ul><li>Instruction</li> 00115 * <li>Instruction_noargs (an Instruction with no embedded arguments)</li> 00116 * <li>Instruction_byte (an Instruction with a single byte data argument)</li> 00117 * <li>Instruction_bytevar (a byte argument specifying a local variable)</li> 00118 * <li>Instruction_byteindex (a byte argument specifying a constant pool index)</li> 00119 * <li>Instruction_int (an Instruction with a single short data argument)</li> 00120 * <li>Instruction_intvar (a short argument specifying a local variable)</li> 00121 * <li>Instruction_intindex (a short argument specifying a constant pool index)</li> 00122 * <li>Instruction_intbranch (a short argument specifying a code offset)</li> 00123 * <li>Instruction_longbranch (an int argument specifying a code offset)</li> 00124 * </ul> 00125 * @author Clark Verbrugge 00126 * @see Instruction 00127 * @see Instruction_noargs 00128 * @see Instruction_byte 00129 * @see Instruction_bytevar 00130 * @see Instruction_byteindex 00131 * @see Instruction_int 00132 * @see Instruction_intvar 00133 * @see Instruction_intindex 00134 * @see Instruction_intbranch 00135 * @see Instruction_longbranch 00136 * @see Instruction_Unknown 00137 */ 00138 class Instruction_Tableswitch extends Instruction { 00139 public byte pad; // number of bytes used for padding 00140 public int default_offset; 00141 public int low; 00142 public int high; 00143 public int jump_offsets[]; 00144 public Instruction default_inst; 00145 public Instruction jump_insts[]; 00146 public Instruction_Tableswitch() { 00147 super((byte)ByteCode.TABLESWITCH); 00148 name = "tableswitch"; 00149 branches = true; 00150 } 00151 public Instruction[] branchpoints(Instruction next) { 00152 Instruction i[] = new Instruction[high-low+2]; 00153 int j; 00154 i[0] = default_inst; 00155 for (j=1;j<high-low+2;j++) 00156 i[j] = jump_insts[j-1]; 00157 return i; 00158 } 00159 public int compile(byte bc[],int index) { 00160 int i; 00161 bc[index++] = code; 00162 // insert padding so next instruction is on a 4-byte boundary 00163 for (i=0;i<pad;i++) 00164 bc[index++] = 0; 00165 if (default_inst!=null) 00166 index = intToBytes(default_inst.label-label,bc,index); 00167 else 00168 index = intToBytes(default_offset,bc,index); 00169 index = intToBytes(low,bc,index); 00170 index = intToBytes(high,bc,index); 00171 for (i=0;i<=high-low;i++) { 00172 if (jump_insts[i]!=null) 00173 index = intToBytes((jump_insts[i]).label-label,bc,index); 00174 else 00175 index = intToBytes(jump_offsets[i],bc,index); 00176 } 00177 return index; 00178 } 00179 public int nextOffset(int curr) { 00180 int i,j,baseindex,siz=0; 00181 baseindex = curr; 00182 i = (curr+1) % 4; 00183 if (i != 0) 00184 siz = (4 - i); 00185 return (curr + siz + 13 + (high-low+1)*4); 00186 } 00187 public void offsetToPointer(ByteCode bc) { 00188 int i; 00189 default_inst = bc.locateInst(default_offset+label); 00190 if (default_inst==null) { 00191 System.out.println("Warning: can't locate target of instruction"); 00192 System.out.println(" which should be at byte address " + (label+default_offset)); 00193 } else 00194 default_inst.labelled = true; 00195 if (high-low+1>0) { 00196 jump_insts = new Instruction[high-low+1]; 00197 for (i=0;i<high-low+1;i++) { 00198 jump_insts[i] = bc.locateInst(jump_offsets[i]+label); 00199 if (jump_insts[i]==null) { 00200 System.out.println("Warning: can't locate target of instruction"); 00201 System.out.println(" which should be at byte address " + 00202 (label+jump_offsets[i])); 00203 } else 00204 jump_insts[i].labelled = true; 00205 } 00206 } 00207 } 00208 public int parse(byte bc[],int index) { 00209 // first figure out padding to next 4-byte quantity 00210 int i,j,baseindex; 00211 baseindex = index; 00212 i = index % 4; 00213 if (i != 0) 00214 pad = (byte)(4 - i); 00215 else 00216 pad = (byte)0; 00217 index += pad; 00218 default_offset = getInt(bc,index); 00219 index += 4; 00220 low = getInt(bc,index); 00221 index += 4; 00222 high = getInt(bc,index); 00223 index += 4; 00224 i = high-low+1; 00225 if (i>0) { 00226 jump_offsets = new int[i]; 00227 j = 0; 00228 do { 00229 jump_offsets[j] = getInt(bc,index); 00230 index += 4; 00231 j++; 00232 } while(j<i); 00233 } 00234 return index; 00235 } 00236 public String toString(cp_info constant_pool[]) { 00237 String args; 00238 int i; 00239 args = super.toString(constant_pool) + argsep + "(" + 00240 Integer.toString(pad) + ")"; 00241 args = args + argsep + "label_" + Integer.toString(default_inst.label); 00242 args = args + argsep + Integer.toString(low); 00243 args = args + argsep + Integer.toString(high) + ": "; 00244 for (i=0;i<high-low+1;i++) { 00245 args = args + argsep + "label_" + Integer.toString(jump_insts[i].label); 00246 } 00247 return args; 00248 } 00249 }