1 23 24 25 package com.sun.jdo.api.persistence.enhancer.classfile; 26 27 28 import java.io.PrintStream ; 29 30 34 35 public class InsnConstOp extends Insn { 36 37 private ConstBasic constValue; 38 39 40 41 public int nStackArgs() { 42 int n = VMOp.ops[opcode()].nStackArgs(); 43 if (n >= 0) 44 return n; 45 switch (opcode()) { 46 case opc_putstatic: 47 case opc_putfield: 48 { 49 ConstFieldRef fld = (ConstFieldRef) constValue; 50 String sig = fld.nameAndType().signature().asString(); 51 if (sig.equals("J") || sig.equals("D")) return (opcode() == opc_putfield) ? 3 : 2; 53 return (opcode() == opc_putfield) ? 2 : 1; 54 } 55 case opc_invokevirtual: 56 case opc_invokespecial: 57 case opc_invokestatic: 58 59 case opc_invokeinterface: 60 { 61 ConstBasicMemberRef meth = (ConstBasicMemberRef) constValue; 62 String sig = meth.nameAndType().signature().asString(); 63 int nMethodArgWords = Descriptor.countMethodArgWords(sig); 64 return nMethodArgWords + 65 ((opcode() == opc_invokestatic) ? 0 : 1); 66 } 67 default: 68 throw new InsnError("unexpected variable opcode"); } 70 } 71 72 public int nStackResults() { 73 int n = VMOp.ops[opcode()].nStackResults(); 74 if (n >= 0) 75 return n; 76 switch (opcode()) { 77 case opc_getstatic: 78 case opc_getfield: 79 { 80 ConstFieldRef fld = (ConstFieldRef) constValue; 81 String sig = fld.nameAndType().signature().asString(); 82 if (sig.equals("J") || sig.equals("D")) return 2; 84 return 1; 85 } 86 case opc_invokevirtual: 87 case opc_invokespecial: 88 case opc_invokestatic: 89 90 case opc_invokeinterface: 91 { 92 ConstBasicMemberRef meth = (ConstBasicMemberRef) constValue; 93 return Descriptor.countMethodReturnWords( 94 meth.nameAndType().signature().asString()); 95 } 96 default: 97 throw new InsnError("unexpected variable opcode"); } 99 } 100 101 public String argTypes() { 102 switch (opcode()) { 103 case opc_putstatic: 104 case opc_putfield: 105 { 106 ConstFieldRef fld = (ConstFieldRef) constValue; 107 String sig = fld.nameAndType().signature().asString(); 108 if (opcode() == opc_putstatic) 109 return sig; 110 else 111 return descriptorTypeOfObject(fld) + sig; 112 } 113 case opc_invokevirtual: 114 case opc_invokespecial: 115 case opc_invokestatic: 116 117 case opc_invokeinterface: 118 { 119 ConstBasicMemberRef meth = (ConstBasicMemberRef) constValue; 120 String argSig = 121 Descriptor.extractArgSig(meth.nameAndType().signature().asString()); 122 if (opcode() == opc_invokestatic) 123 return argSig; 124 else 125 return descriptorTypeOfObject(meth) + argSig; 126 } 127 default: 128 return VMOp.ops[opcode()].argTypes(); 129 } 130 } 131 132 public String resultTypes() { 133 switch (opcode()) { 134 case opc_invokevirtual: 135 case opc_invokespecial: 136 case opc_invokestatic: 137 138 case opc_invokeinterface: 139 { 140 ConstBasicMemberRef meth = (ConstBasicMemberRef) constValue; 141 String resultSig = Descriptor.extractResultSig( 142 meth.nameAndType().signature().asString()); 143 if (resultSig.equals("V")) return ""; return resultSig; 146 } 147 case opc_getstatic: 148 case opc_getfield: 149 { 150 ConstFieldRef fld = (ConstFieldRef) constValue; 151 return fld.nameAndType().signature().asString(); 152 } 153 case opc_ldc: 154 case opc_ldc_w: 155 case opc_ldc2_w: 156 { 157 ConstValue constVal = (ConstValue) constValue; 158 return constVal.descriptor(); 159 } 160 default: 161 return VMOp.ops[opcode()].resultTypes(); 162 } 163 } 164 165 public boolean branches() { 166 167 return false; 168 } 169 170 173 public ConstBasic value() { 174 return constValue; 175 } 176 177 180 public void setValue(ConstBasic newValue) { 181 checkConstant(newValue); 182 constValue = newValue; 183 } 184 185 186 187 void print (PrintStream out, int indent) { 188 ClassPrint.spaces(out, indent); 189 out.println(offset() + " " + opName(opcode()) + " pool(" + constValue.getIndex() + ")"); } 192 193 int store(byte[] buf, int index) { 194 if (opcode() == opc_ldc && !isNarrowldc()) 195 buf[index++] = (byte) opc_ldc_w; 196 else 197 buf[index++] = (byte) opcode(); 198 int constIndex = constValue.getIndex(); 199 if (size() == 3) 200 buf[index++] = (byte) (constIndex >> 8); 201 buf[index++] = (byte)(constIndex & 0xff); 202 return index; 203 } 204 205 int size() { 206 return isNarrowldc() ? 2 : 3; 207 } 208 209 private boolean isNarrowldc() { 210 return (opcode() == opc_ldc && constValue.getIndex() < 256); 211 } 212 213 214 InsnConstOp (int theOpcode, ConstBasic theOperand) { 215 this(theOpcode, theOperand, NO_OFFSET); 216 } 217 218 InsnConstOp (int theOpcode, ConstBasic theOperand, int pc) { 219 super(theOpcode, pc); 220 constValue = theOperand; 221 checkConstant(theOperand); 222 if (theOpcode == opc_invokeinterface) 223 throw new InsnError("attempt to create an " + opName(theOpcode) + " as an InsnConstOp instead of InsnInterfaceInvoke"); } 226 227 229 InsnConstOp (int theOpcode, ConstInterfaceMethodRef theOperand, int pc) { 230 super(theOpcode, pc); 231 constValue = theOperand; 232 checkConstant(theOperand); 233 } 234 235 private void checkConstant (ConstBasic operand) { 236 switch(opcode()) { 237 case opc_ldc: 238 case opc_ldc_w: 239 case opc_ldc2_w: 240 241 if (operand == null || 242 (! (operand instanceof ConstValue))) 243 throw new InsnError ("attempt to create an " + opName(opcode()) + " without a ConstValue operand"); break; 246 247 case opc_getstatic: 248 case opc_putstatic: 249 case opc_getfield: 250 case opc_putfield: 251 252 if (operand == null || 253 (! (operand instanceof ConstFieldRef))) 254 throw new InsnError ("attempt to create an " + opName(opcode()) + " without a ConstFieldRef operand"); break; 257 258 case opc_invokevirtual: 259 case opc_invokespecial: 260 case opc_invokestatic: 261 262 if (operand == null || 263 (! (operand instanceof ConstMethodRef))) 264 throw new InsnError ("attempt to create an " + opName(opcode()) + " without a ConstMethodRef operand"); break; 267 268 case opc_invokeinterface: 269 270 if (operand == null || 271 (! (operand instanceof ConstInterfaceMethodRef))) 272 throw new InsnError("Attempt to create an " + opName(opcode()) + " without a ConstInterfaceMethodRef operand"); break; 275 276 case opc_new: 277 case opc_anewarray: 278 case opc_checkcast: 279 case opc_instanceof: 280 281 if (operand == null || 282 (! (operand instanceof ConstClass))) 283 throw new InsnError ("attempt to create an " + opName(opcode()) + " without a ConstClass operand"); break; 286 287 default: 288 throw new InsnError ("attempt to create an " + opName(opcode()) + " with a constant operand"); } 291 } 292 293 private final String descriptorTypeOfObject(ConstBasicMemberRef memRef) { 294 String cname = memRef.className().className().asString(); 295 return "L" + cname + ";"; } 297 298 } 299 | Popular Tags |