1 19 20 25 26 27 28 29 30 package soot.baf.internal; 31 32 import soot.*; 33 import soot.baf.*; 34 import soot.util.*; 35 import java.util.*; 36 37 public class BPrimitiveCastInst extends AbstractInst 38 implements PrimitiveCastInst 39 { 40 Type fromType; 41 42 protected Type toType; 43 44 public int getInCount() 45 { 46 return 1; 47 } 48 49 public int getInMachineCount() 50 { 51 return JasminClass.sizeOfType(fromType); 52 } 53 54 public int getOutCount() 55 { 56 return 1; 57 } 58 59 public int getOutMachineCount() 60 { 61 return JasminClass.sizeOfType(toType); 62 } 63 64 65 public BPrimitiveCastInst(Type fromType, Type toType) 66 { 67 68 if( fromType instanceof NullType ) 69 throw new RuntimeException ("invalid fromType " + fromType); 70 this.fromType = fromType; 71 this.toType = toType; 72 } 73 74 75 public Object clone() 76 { 77 return new BPrimitiveCastInst(getFromType(), toType); 78 } 79 80 81 82 public Type getFromType() { return fromType; } 84 public void setFromType(Type t) { fromType = t;} 85 86 public Type getToType() { return toType; } 87 public void setToType(Type t) { toType = t;} 88 89 final public String getName() 90 { 91 TypeSwitch sw; 92 93 fromType.apply(sw = new TypeSwitch() 94 { 95 public void defaultCase(Type ty) 96 { 97 throw new RuntimeException ("invalid fromType " + fromType); 98 } 99 100 public void caseDoubleType(DoubleType ty) 101 { 102 if(toType.equals(IntType.v())) 103 setResult("d2i"); 104 else if(toType.equals(LongType.v())) 105 setResult("d2l"); 106 else if(toType.equals(FloatType.v())) 107 setResult("d2f"); 108 else 109 throw new RuntimeException 110 ("invalid toType from double: " + toType); 111 } 112 113 public void caseFloatType(FloatType ty) 114 { 115 if(toType.equals(IntType.v())) 116 setResult("f2i"); 117 else if(toType.equals(LongType.v())) 118 setResult("f2l"); 119 else if(toType.equals(DoubleType.v())) 120 setResult("f2d"); 121 else 122 throw new RuntimeException 123 ("invalid toType from float: " + toType); 124 } 125 126 public void caseIntType(IntType ty) 127 { 128 emitIntToTypeCast(); 129 } 130 131 public void caseBooleanType(BooleanType ty) 132 { 133 emitIntToTypeCast(); 134 } 135 136 public void caseByteType(ByteType ty) 137 { 138 emitIntToTypeCast(); 139 } 140 141 public void caseCharType(CharType ty) 142 { 143 emitIntToTypeCast(); 144 } 145 146 public void caseShortType(ShortType ty) 147 { 148 emitIntToTypeCast(); 149 } 150 151 private void emitIntToTypeCast() 152 { 153 if(toType.equals(ByteType.v())) 154 setResult("i2b"); 155 else if(toType.equals(CharType.v())) 156 setResult("i2c"); 157 else if(toType.equals(ShortType.v())) 158 setResult("i2s"); 159 else if(toType.equals(FloatType.v())) 160 setResult("i2f"); 161 else if(toType.equals(LongType.v())) 162 setResult("i2l"); 163 else if(toType.equals(DoubleType.v())) 164 setResult("i2d"); 165 else if(toType.equals(IntType.v())) 166 setResult(""); else if(toType.equals(BooleanType.v())) 168 setResult(""); 169 else 170 throw new RuntimeException 171 ("invalid toType from int: " + toType); 172 } 173 174 public void caseLongType(LongType ty) 175 { 176 if(toType.equals(IntType.v())) 177 setResult("l2i"); 178 else if(toType.equals(FloatType.v())) 179 setResult("l2f"); 180 else if(toType.equals(DoubleType.v())) 181 setResult("l2d"); 182 else 183 throw new RuntimeException 184 ("invalid toType from long: " + toType); 185 186 } 187 }); 188 return (String )sw.getResult(); 189 } 190 191 192 public String toString() 193 { 194 return getName() + 195 getParameters(); 196 } 197 198 public void apply(Switch sw) 199 { 200 ((InstSwitch) sw).casePrimitiveCastInst(this); 201 } 202 } 203 204 | Popular Tags |