1.1 --- a/src/share/classes/com/sun/tools/javap/CodeWriter.java Wed Mar 25 10:29:28 2009 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javap/CodeWriter.java Mon Mar 30 15:08:09 2009 -0700 1.3 @@ -30,9 +30,12 @@ 1.4 import com.sun.tools.classfile.ConstantPool; 1.5 import com.sun.tools.classfile.ConstantPoolException; 1.6 import com.sun.tools.classfile.DescriptorException; 1.7 +import com.sun.tools.classfile.Instruction; 1.8 +import com.sun.tools.classfile.Instruction.TypeKind; 1.9 import com.sun.tools.classfile.Method; 1.10 +import com.sun.tools.classfile.Opcode; 1.11 1.12 -import static com.sun.tools.classfile.OpCodes.*; 1.13 +//import static com.sun.tools.classfile.OpCodes.*; 1.14 1.15 /* 1.16 * Write the contents of a Code attribute. 1.17 @@ -87,218 +90,92 @@ 1.18 } 1.19 1.20 public void writeInstrs(Code_attribute attr) { 1.21 - try { 1.22 - for (int pc = 0; pc < attr.code_length;) { 1.23 - print(" " + pc + ":\t"); 1.24 - pc += writeInstr(attr, pc); 1.25 - println(); 1.26 + for (Instruction instr: attr.getInstructions()) { 1.27 + try { 1.28 + writeInstr(instr); 1.29 + } catch (ArrayIndexOutOfBoundsException e) { 1.30 + println(report("error at or after byte " + instr.getPC())); 1.31 + break; 1.32 } 1.33 - } catch (Code_attribute.InvalidIndex e) { 1.34 - println(report(e)); 1.35 } 1.36 } 1.37 1.38 - public int writeInstr(Code_attribute attr, int pc) 1.39 - throws Code_attribute.InvalidIndex { 1.40 - String lP = ""; 1.41 - int opcode = attr.getUnsignedByte(pc); 1.42 - int opcode2; 1.43 - String mnem; 1.44 - switch (opcode) { 1.45 - case opc_nonpriv: 1.46 - case opc_priv: { 1.47 - opcode2 = attr.getUnsignedByte(pc + 1); 1.48 - mnem = opcName((opcode << 8) + opcode2); 1.49 - if (mnem == null) { 1.50 - mnem = opcName(opcode) + " " + opcode2; 1.51 - } 1.52 - print(mnem); 1.53 - return 2; 1.54 + public void writeInstr(Instruction instr) { 1.55 + print(" " + instr.getPC() + ":\t"); 1.56 + print(instr.getMnemonic()); 1.57 + instr.accept(instructionPrinter, null); 1.58 + println(); 1.59 + } 1.60 + // where 1.61 + Instruction.KindVisitor<Void,Void> instructionPrinter = 1.62 + new Instruction.KindVisitor<Void,Void>() { 1.63 + 1.64 + public Void visitNoOperands(Instruction instr, Void p) { 1.65 + return null; 1.66 + } 1.67 + 1.68 + public Void visitArrayType(Instruction instr, TypeKind kind, Void p) { 1.69 + print(" " + kind.name); 1.70 + return null; 1.71 + } 1.72 + 1.73 + public Void visitBranch(Instruction instr, int offset, Void p) { 1.74 + print("\t" + (instr.getPC() + offset)); 1.75 + return null; 1.76 + } 1.77 + 1.78 + public Void visitConstantPoolRef(Instruction instr, int index, Void p) { 1.79 + print("\t#" + index + "; //"); 1.80 + printConstant(index); 1.81 + return null; 1.82 + } 1.83 + 1.84 + public Void visitConstantPoolRefAndValue(Instruction instr, int index, int value, Void p) { 1.85 + print("\t#" + index + ", " + value + "; //"); 1.86 + printConstant(index); 1.87 + return null; 1.88 + } 1.89 + 1.90 + public Void visitLocal(Instruction instr, int index, Void p) { 1.91 + print("\t" + index); 1.92 + return null; 1.93 + } 1.94 + 1.95 + public Void visitLocalAndValue(Instruction instr, int index, int value, Void p) { 1.96 + print("\t" + index + ", " + value); 1.97 + return null; 1.98 + } 1.99 + 1.100 + public Void visitLookupSwitch(Instruction instr, int default_, int npairs, int[] matches, int[] offsets) { 1.101 + int pc = instr.getPC(); 1.102 + print("{ //" + npairs); 1.103 + for (int i = 0; i < npairs; i++) { 1.104 + print("\n\t\t" + matches[i] + ": " + (pc + offsets[i]) + ";"); 1.105 } 1.106 - case opc_wide: { 1.107 - opcode2 = attr.getUnsignedByte(pc + 1); 1.108 - mnem = opcName((opcode << 8) + opcode2); 1.109 - if (mnem == null) { 1.110 - print("bytecode " + opcode); 1.111 - return 1; 1.112 - } 1.113 - print(mnem + " " + attr.getUnsignedShort(pc + 2)); 1.114 - if (opcode2 == opc_iinc) { 1.115 - print(", " + attr.getShort(pc + 4)); 1.116 - return 6; 1.117 - } 1.118 - return 4; 1.119 + print("\n\t\tdefault: " + (pc + default_) + " }"); 1.120 + return null; 1.121 + } 1.122 + 1.123 + public Void visitTableSwitch(Instruction instr, int default_, int low, int high, int[] offsets) { 1.124 + int pc = instr.getPC(); 1.125 + print("{ //" + low + " to " + high); 1.126 + for (int i = 0; i < offsets.length; i++) { 1.127 + print("\n\t\t" + (low + i) + ": " + (pc + offsets[i]) + ";"); 1.128 } 1.129 + print("\n\t\tdefault: " + (pc + default_) + " }"); 1.130 + return null; 1.131 } 1.132 - mnem = opcName(opcode); 1.133 - if (mnem == null) { 1.134 - print("bytecode " + opcode); 1.135 - return 1; 1.136 + 1.137 + public Void visitValue(Instruction instr, int value, Void p) { 1.138 + print("\t" + value); 1.139 + return null; 1.140 } 1.141 - if (opcode > opc_jsr_w) { 1.142 - print("bytecode " + opcode); 1.143 - return 1; 1.144 + 1.145 + public Void visitUnknown(Instruction instr, Void p) { 1.146 + return null; 1.147 } 1.148 - print(opcName(opcode)); 1.149 - switch (opcode) { 1.150 - case opc_aload: 1.151 - case opc_astore: 1.152 - case opc_fload: 1.153 - case opc_fstore: 1.154 - case opc_iload: 1.155 - case opc_istore: 1.156 - case opc_lload: 1.157 - case opc_lstore: 1.158 - case opc_dload: 1.159 - case opc_dstore: 1.160 - case opc_ret: 1.161 - print("\t" + attr.getUnsignedByte(pc + 1)); 1.162 - return 2; 1.163 - case opc_iinc: 1.164 - print("\t" + attr.getUnsignedByte(pc + 1) + ", " + attr.getByte(pc + 2)); 1.165 - return 3; 1.166 - case opc_tableswitch: 1.167 - { 1.168 - int tb = align(pc + 1); 1.169 - int default_skip = attr.getInt(tb); 1.170 - int low = attr.getInt(tb + 4); 1.171 - int high = attr.getInt(tb + 8); 1.172 - int count = high - low; 1.173 - print("{ //" + low + " to " + high); 1.174 - for (int i = 0; i <= count; i++) { 1.175 - print("\n\t\t" + (i + low) + ": " + lP + (pc + attr.getInt(tb + 12 + 4 * i)) + ";"); 1.176 - } 1.177 - print("\n\t\tdefault: " + lP + (default_skip + pc) + " }"); 1.178 - return tb - pc + 16 + count * 4; 1.179 - } 1.180 - case opc_lookupswitch: 1.181 - { 1.182 - int tb = align(pc + 1); 1.183 - int default_skip = attr.getInt(tb); 1.184 - int npairs = attr.getInt(tb + 4); 1.185 - print("{ //" + npairs); 1.186 - for (int i = 1; i <= npairs; i++) { 1.187 - print("\n\t\t" + attr.getInt(tb + i * 8) + ": " + lP + (pc + attr.getInt(tb + 4 + i * 8)) + ";"); 1.188 - } 1.189 - print("\n\t\tdefault: " + lP + (default_skip + pc) + " }"); 1.190 - return tb - pc + (npairs + 1) * 8; 1.191 - } 1.192 - case opc_newarray: 1.193 - int type = attr.getUnsignedByte(pc + 1); 1.194 - switch (type) { 1.195 - case T_BOOLEAN: 1.196 - print(" boolean"); 1.197 - break; 1.198 - case T_BYTE: 1.199 - print(" byte"); 1.200 - break; 1.201 - case T_CHAR: 1.202 - print(" char"); 1.203 - break; 1.204 - case T_SHORT: 1.205 - print(" short"); 1.206 - break; 1.207 - case T_INT: 1.208 - print(" int"); 1.209 - break; 1.210 - case T_LONG: 1.211 - print(" long"); 1.212 - break; 1.213 - case T_FLOAT: 1.214 - print(" float"); 1.215 - break; 1.216 - case T_DOUBLE: 1.217 - print(" double"); 1.218 - break; 1.219 - case T_CLASS: 1.220 - print(" class"); 1.221 - break; 1.222 - default: 1.223 - print(" BOGUS TYPE:" + type); 1.224 - } 1.225 - return 2; 1.226 - case opc_anewarray: 1.227 - { 1.228 - int index = attr.getUnsignedShort(pc + 1); 1.229 - print("\t#" + index + "; //"); 1.230 - printConstant(index); 1.231 - return 3; 1.232 - } 1.233 - case opc_sipush: 1.234 - print("\t" + attr.getShort(pc + 1)); 1.235 - return 3; 1.236 - case opc_bipush: 1.237 - print("\t" + attr.getByte(pc + 1)); 1.238 - return 2; 1.239 - case opc_ldc: 1.240 - { 1.241 - int index = attr.getUnsignedByte(pc + 1); 1.242 - print("\t#" + index + "; //"); 1.243 - printConstant(index); 1.244 - return 2; 1.245 - } 1.246 - case opc_ldc_w: 1.247 - case opc_ldc2_w: 1.248 - case opc_instanceof: 1.249 - case opc_checkcast: 1.250 - case opc_new: 1.251 - case opc_putstatic: 1.252 - case opc_getstatic: 1.253 - case opc_putfield: 1.254 - case opc_getfield: 1.255 - case opc_invokevirtual: 1.256 - case opc_invokespecial: 1.257 - case opc_invokestatic: 1.258 - { 1.259 - int index = attr.getUnsignedShort(pc + 1); 1.260 - print("\t#" + index + "; //"); 1.261 - printConstant(index); 1.262 - return 3; 1.263 - } 1.264 - case opc_invokeinterface: 1.265 - { 1.266 - int index = attr.getUnsignedShort(pc + 1); 1.267 - int nargs = attr.getUnsignedByte(pc + 3); 1.268 - print("\t#" + index + ", " + nargs + "; //"); 1.269 - printConstant(index); 1.270 - return 5; 1.271 - } 1.272 - case opc_multianewarray: 1.273 - { 1.274 - int index = attr.getUnsignedShort(pc + 1); 1.275 - int dimensions = attr.getUnsignedByte(pc + 3); 1.276 - print("\t#" + index + ", " + dimensions + "; //"); 1.277 - printConstant(index); 1.278 - return 4; 1.279 - } 1.280 - case opc_jsr: 1.281 - case opc_goto: 1.282 - case opc_ifeq: 1.283 - case opc_ifge: 1.284 - case opc_ifgt: 1.285 - case opc_ifle: 1.286 - case opc_iflt: 1.287 - case opc_ifne: 1.288 - case opc_if_icmpeq: 1.289 - case opc_if_icmpne: 1.290 - case opc_if_icmpge: 1.291 - case opc_if_icmpgt: 1.292 - case opc_if_icmple: 1.293 - case opc_if_icmplt: 1.294 - case opc_if_acmpeq: 1.295 - case opc_if_acmpne: 1.296 - case opc_ifnull: 1.297 - case opc_ifnonnull: 1.298 - print("\t" + lP + (pc + attr.getShort(pc + 1))); 1.299 - return 3; 1.300 - case opc_jsr_w: 1.301 - case opc_goto_w: 1.302 - print("\t" + lP + (pc + attr.getInt(pc + 1))); 1.303 - return 5; 1.304 - default: 1.305 - return 1; 1.306 - } 1.307 - } 1.308 + }; 1.309 + 1.310 1.311 public void writeExceptionTable(Code_attribute attr) { 1.312 if (attr.exception_table_langth > 0) {