1 16 19 20 package com.sun.org.apache.xalan.internal.xsltc.compiler.util; 21 22 import com.sun.org.apache.bcel.internal.generic.BranchHandle; 23 import com.sun.org.apache.bcel.internal.generic.BranchInstruction; 24 import com.sun.org.apache.bcel.internal.generic.CHECKCAST; 25 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen; 26 import com.sun.org.apache.bcel.internal.generic.GOTO; 27 import com.sun.org.apache.bcel.internal.generic.IFEQ; 28 import com.sun.org.apache.bcel.internal.generic.IFGE; 29 import com.sun.org.apache.bcel.internal.generic.IFGT; 30 import com.sun.org.apache.bcel.internal.generic.IFLE; 31 import com.sun.org.apache.bcel.internal.generic.IFLT; 32 import com.sun.org.apache.bcel.internal.generic.IF_ICMPGE; 33 import com.sun.org.apache.bcel.internal.generic.IF_ICMPGT; 34 import com.sun.org.apache.bcel.internal.generic.IF_ICMPLE; 35 import com.sun.org.apache.bcel.internal.generic.IF_ICMPLT; 36 import com.sun.org.apache.bcel.internal.generic.ILOAD; 37 import com.sun.org.apache.bcel.internal.generic.INVOKESPECIAL; 38 import com.sun.org.apache.bcel.internal.generic.INVOKESTATIC; 39 import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL; 40 import com.sun.org.apache.bcel.internal.generic.ISTORE; 41 import com.sun.org.apache.bcel.internal.generic.Instruction; 42 import com.sun.org.apache.bcel.internal.generic.InstructionConstants; 43 import com.sun.org.apache.bcel.internal.generic.InstructionList; 44 import com.sun.org.apache.bcel.internal.generic.NEW; 45 import com.sun.org.apache.xalan.internal.xsltc.compiler.Constants; 46 import com.sun.org.apache.xalan.internal.xsltc.compiler.FlowList; 47 48 52 public final class IntType extends NumberType { 53 protected IntType() {} 54 55 public String toString() { 56 return "int"; 57 } 58 59 public boolean identicalTo(Type other) { 60 return this == other; 61 } 62 63 public String toSignature() { 64 return "I"; 65 } 66 67 public com.sun.org.apache.bcel.internal.generic.Type toJCType() { 68 return com.sun.org.apache.bcel.internal.generic.Type.INT; 69 } 70 71 74 public int distanceTo(Type type) { 75 if (type == this) { 76 return 0; 77 } 78 else if (type == Type.Real) { 79 return 1; 80 } 81 else 82 return Integer.MAX_VALUE; 83 } 84 85 90 public void translateTo(ClassGenerator classGen, MethodGenerator methodGen, 91 final Type type) { 92 if (type == Type.Real) { 93 translateTo(classGen, methodGen, (RealType) type); 94 } 95 else if (type == Type.String) { 96 translateTo(classGen, methodGen, (StringType) type); 97 } 98 else if (type == Type.Boolean) { 99 translateTo(classGen, methodGen, (BooleanType) type); 100 } 101 else if (type == Type.Reference) { 102 translateTo(classGen, methodGen, (ReferenceType) type); 103 } 104 else { 105 ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR, 106 toString(), type.toString()); 107 classGen.getParser().reportError(Constants.FATAL, err); 108 } 109 } 110 111 116 public void translateTo(ClassGenerator classGen, MethodGenerator methodGen, 117 RealType type) { 118 methodGen.getInstructionList().append(I2D); 119 } 120 121 127 public void translateTo(ClassGenerator classGen, MethodGenerator methodGen, 128 StringType type) { 129 final ConstantPoolGen cpg = classGen.getConstantPool(); 130 final InstructionList il = methodGen.getInstructionList(); 131 il.append(new INVOKESTATIC(cpg.addMethodref(INTEGER_CLASS, 132 "toString", 133 "(I)" + STRING_SIG))); 134 } 135 136 142 public void translateTo(ClassGenerator classGen, MethodGenerator methodGen, 143 BooleanType type) { 144 final InstructionList il = methodGen.getInstructionList(); 145 final BranchHandle falsec = il.append(new IFEQ(null)); 146 il.append(ICONST_1); 147 final BranchHandle truec = il.append(new GOTO(null)); 148 falsec.setTarget(il.append(ICONST_0)); 149 truec.setTarget(il.append(NOP)); 150 } 151 152 159 public FlowList translateToDesynthesized(ClassGenerator classGen, 160 MethodGenerator methodGen, 161 BooleanType type) { 162 final InstructionList il = methodGen.getInstructionList(); 163 return new FlowList(il.append(new IFEQ(null))); 164 } 165 166 173 public void translateTo(ClassGenerator classGen, MethodGenerator methodGen, 174 ReferenceType type) { 175 final ConstantPoolGen cpg = classGen.getConstantPool(); 176 final InstructionList il = methodGen.getInstructionList(); 177 il.append(new NEW(cpg.addClass(INTEGER_CLASS))); 178 il.append(DUP_X1); 179 il.append(SWAP); 180 il.append(new INVOKESPECIAL(cpg.addMethodref(INTEGER_CLASS, 181 "<init>", "(I)V"))); 182 } 183 184 189 public void translateTo(ClassGenerator classGen, MethodGenerator methodGen, 190 Class clazz) { 191 final InstructionList il = methodGen.getInstructionList(); 192 if (clazz == Character.TYPE) { 193 il.append(I2C); 194 } 195 else if (clazz == Byte.TYPE) { 196 il.append(I2B); 197 } 198 else if (clazz == Short.TYPE) { 199 il.append(I2S); 200 } 201 else if (clazz == Integer.TYPE) { 202 il.append(NOP); 203 } 204 else if (clazz == Long.TYPE) { 205 il.append(I2L); 206 } 207 else if (clazz == Float.TYPE) { 208 il.append(I2F); 209 } 210 else if (clazz == Double.TYPE) { 211 il.append(I2D); 212 } 213 else if (clazz.isAssignableFrom(java.lang.Double .class)) { 215 il.append(I2D); 216 Type.Real.translateTo(classGen, methodGen, Type.Reference); 217 } 218 else { 219 ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR, 220 toString(), clazz.getName()); 221 classGen.getParser().reportError(Constants.FATAL, err); 222 } 223 } 224 225 228 public void translateBox(ClassGenerator classGen, 229 MethodGenerator methodGen) { 230 translateTo(classGen, methodGen, Type.Reference); 231 } 232 233 236 public void translateUnBox(ClassGenerator classGen, 237 MethodGenerator methodGen) { 238 final ConstantPoolGen cpg = classGen.getConstantPool(); 239 final InstructionList il = methodGen.getInstructionList(); 240 il.append(new CHECKCAST(cpg.addClass(INTEGER_CLASS))); 241 final int index = cpg.addMethodref(INTEGER_CLASS, 242 INT_VALUE, 243 INT_VALUE_SIG); 244 il.append(new INVOKEVIRTUAL(index)); 245 } 246 247 public Instruction ADD() { 248 return InstructionConstants.IADD; 249 } 250 251 public Instruction SUB() { 252 return InstructionConstants.ISUB; 253 } 254 255 public Instruction MUL() { 256 return InstructionConstants.IMUL; 257 } 258 259 public Instruction DIV() { 260 return InstructionConstants.IDIV; 261 } 262 263 public Instruction REM() { 264 return InstructionConstants.IREM; 265 } 266 267 public Instruction NEG() { 268 return InstructionConstants.INEG; 269 } 270 271 public Instruction LOAD(int slot) { 272 return new ILOAD(slot); 273 } 274 275 public Instruction STORE(int slot) { 276 return new ISTORE(slot); 277 } 278 279 public BranchInstruction GT(boolean tozero) { 280 return tozero ? (BranchInstruction) new IFGT(null) : 281 (BranchInstruction) new IF_ICMPGT(null); 282 } 283 284 public BranchInstruction GE(boolean tozero) { 285 return tozero ? (BranchInstruction) new IFGE(null) : 286 (BranchInstruction) new IF_ICMPGE(null); 287 } 288 289 public BranchInstruction LT(boolean tozero) { 290 return tozero ? (BranchInstruction) new IFLT(null) : 291 (BranchInstruction) new IF_ICMPLT(null); 292 } 293 294 public BranchInstruction LE(boolean tozero) { 295 return tozero ? (BranchInstruction) new IFLE(null) : 296 (BranchInstruction) new IF_ICMPLE(null); 297 } 298 } 299 | Popular Tags |