1 // ASM: a very small and fast Java bytecode manipulation framework 2 // Copyright (c) 2000-2011 INRIA, France Telecom 3 // All rights reserved. 4 // 5 // Redistribution and use in source and binary forms, with or without 6 // modification, are permitted provided that the following conditions 7 // are met: 8 // 1. Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // 2. Redistributions in binary form must reproduce the above copyright 11 // notice, this list of conditions and the following disclaimer in the 12 // documentation and/or other materials provided with the distribution. 13 // 3. Neither the name of the copyright holders nor the names of its 14 // contributors may be used to endorse or promote products derived from 15 // this software without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 // THE POSSIBILITY OF SUCH DAMAGE. 28 package jdk5; 29 30 /** 31 * Class which, compiled with the JDK 1.5.0, produces the new JVM instructions from that version 32 * (compared to JDK 1.3.0). Also contain all the other instructions that can be used in JDK 1.5 33 * classes (which excludes jsr and ret), so that ASM classes which don't support jsr or ret (and 34 * therefore can't be tested with jdk3.AllInstructions) can still be tested on all instructions. 35 * Must be compiled with "javac -g". 36 */ 37 class AllInstructions { 38 private Class c; 39 private Class d; 40 private int f; 41 private long g; 42 private AllInstructions field; 43 private static AllInstructions staticField; 44 AllInstructions()45 AllInstructions() {} 46 AllInstructions(int v0, float v1, long v2, double v3, Object v4)47 AllInstructions(int v0, float v1, long v2, double v3, Object v4) {} 48 49 // New instruction in JDK 1.5. ldcWithClassConstant()50 public void ldcWithClassConstant() { 51 c = AllInstructions.class; 52 d = AllInstructions[].class; 53 } 54 intInstructions( int v0, int v1, int v2, int v3, int v4, int v5, int v6, int v7, int v8)55 public static int intInstructions( 56 int v0, int v1, int v2, int v3, int v4, int v5, int v6, int v7, int v8) { 57 boolean b0 = v0 < -1; 58 boolean b1 = v1 > 1; 59 boolean b2 = v2 <= 2; 60 boolean b3 = v3 >= 3; 61 boolean b4 = v4 == 4; 62 boolean b5 = v5 != 5; 63 v0 = b0 ? (v6 + 5) : (v6 - 5); 64 v1 = b1 ? (v7 * 100) : (v7 / 100); 65 v2 = b2 ? (v8 % 10000) : (~v8); 66 v3 = b3 ? (v0 & 1000000) : (v0 | 1000000); 67 v4 = b4 ? (v1 ^ v2) : (v1 << v2); 68 v5 = b5 ? (v2 >> v3) : (v2 >>> v3); 69 v6 += 1; 70 v7 = v6 < 0 ? -v6 : v6; 71 v1 = v0 < 0 ? v1 : v2; 72 v2 = v1 > 0 ? v2 : v3; 73 v3 = v2 <= 0 ? v3 : v4; 74 v4 = v3 >= 0 ? v4 : v5; 75 v5 = v4 == 0 ? v5 : v6; 76 v6 = v5 != 0 ? v6 : v7; 77 return v0 + v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8; 78 } 79 80 public static long longInstructions( 81 long v0, long v1, long v2, long v3, long v4, long v5, long v6, long v7, long v8) { 82 boolean b0 = v0 < -1L; 83 boolean b1 = v1 > 1L; 84 boolean b2 = v2 <= 2L; 85 boolean b3 = v3 >= 3L; 86 boolean b4 = v4 == 4L; 87 boolean b5 = v5 != 5L; 88 v0 = b0 ? (v6 + 5L) : (v6 - 5L); 89 v1 = b1 ? (v7 * 100L) : (v7 / 100L); 90 v2 = b2 ? (v8 % 10000L) : (~v8); 91 v3 = b3 ? (v0 & 1000000L) : (v0 | 1000000L); 92 v4 = b4 ? (v1 ^ v2) : (v1 << v2); 93 v5 = b5 ? (v2 >> v3) : (v2 >>> v3); 94 v6 += 1L; 95 v7 = v6 < 0L ? -v6 : v6; 96 return v0 + v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8; 97 } 98 99 public static float floatInstructions( 100 float v0, float v1, float v2, float v3, float v4, float v5, float v6, float v7, float v8) { 101 boolean b0 = v0 < -1f; 102 boolean b1 = v1 > 1f; 103 boolean b2 = v2 <= 2f; 104 boolean b3 = v3 >= 3f; 105 boolean b4 = v4 == 4f; 106 boolean b5 = v5 != 5f; 107 v0 = b0 ? (v6 + 5f) : (v6 - 5f); 108 v1 = b1 ? (v7 * 100f) : (v7 / 100f); 109 v2 = b2 ? (v8 % 10000f) : v8; 110 v3 = b3 ? -v3 : v3; 111 v4 = b4 ? -v4 : v4; 112 v5 = b5 ? -v5 : v5; 113 v6 += 1f; 114 v7 = v6 < 0f ? -v6 : v6; 115 v8 = v7; 116 return v0 + v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8; 117 } 118 119 public static double doubleInstructions( 120 double v0, 121 double v1, 122 double v2, 123 double v3, 124 double v4, 125 double v5, 126 double v6, 127 double v7, 128 double v8) { 129 boolean b0 = v0 < -1d; 130 boolean b1 = v1 > 1d; 131 boolean b2 = v2 <= 2d; 132 boolean b3 = v3 >= 3d; 133 boolean b4 = v4 == 4d; 134 boolean b5 = v5 != 5d; 135 v0 = b0 ? (v6 + 5d) : (v6 - 5d); 136 v1 = b1 ? (v7 * 100d) : (v7 / 100d); 137 v2 = b2 ? (v8 % 10000d) : v8; 138 v3 = b3 ? -v3 : v3; 139 v4 = b4 ? -v4 : v4; 140 v5 = b5 ? -v5 : v5; 141 v6 += 1d; 142 v7 = v6 < 0d ? -v6 : v6; 143 return v0 + v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8; 144 } 145 146 public static double castInstructions(int v0, long v1, long v2) { 147 byte v3 = (byte) v0; 148 char v4 = (char) v1; 149 short v5 = (short) v2; 150 long v6 = (long) v3; 151 float v7 = (float) v4; 152 double v8 = (double) v5; 153 v1 = v6; 154 v2 = v1; 155 v6 = (long) v8; 156 return v0 + v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8; 157 } 158 159 public static float castInstructions(float v0, double v1, double v2) { 160 byte v3 = (byte) v0; 161 char v4 = (char) v1; 162 short v5 = (short) v2; 163 long v6 = (long) v3; 164 float v7 = (float) v4; 165 double v8 = (double) v5; 166 v1 = v6; 167 v2 = v1; 168 v6 = (long) v7; 169 return (float) (v0 + v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8); 170 } 171 172 public static Object objectInstructions(Object v0, Object v1, Object v2, Object v3, Object v4) { 173 boolean b0 = v0 == v1; 174 boolean b1 = v1 != v2; 175 boolean b2 = v2 == null; 176 boolean b3 = v3 != null; 177 boolean b4 = v4 instanceof String; 178 v0 = b0 ? null : v0; 179 v1 = b1 ? v1 : v0; 180 v2 = b2 ? v2 : v1; 181 v3 = b3 ? v3 : v2; 182 v4 = b4 ? new Integer(((String) v4).length()) : v3; 183 return v4; 184 } 185 186 public static Object[] arrayInstructions( 187 byte[] v0, char[] v1, short[] v2, int[] v3, long[] v4, float[] v5, double[] v6, Object[] v7) { 188 v0[1] = v0[0]; 189 v1[1] = v1[0]; 190 v2[1] = v2[0]; 191 v3[1] = v3[0]; 192 v4[1] = v4[0]; 193 v5[1] = v5[0]; 194 v6[1] = v6[0]; 195 v7[1] = v7[0]; 196 Object[] v8 = new Object[v7.length]; 197 v8[0] = new int[4][8][16]; 198 return v8; 199 } 200 201 public void fieldInstructions() { 202 AllInstructions c = field; 203 field = staticField; 204 staticField = c; 205 } 206 207 public void methodInstructions(Runnable v0) { 208 AllInstructions c = new AllInstructions(); 209 c.fieldInstructions(); 210 monitorInstructions(c); 211 v0.run(); 212 } 213 214 public static int lookupSwitchInstruction(int v0) { 215 switch (v0) { 216 case 1000: 217 return 1; 218 case 10000: 219 return 2; 220 case 100000: 221 return 3; 222 default: 223 return -1; 224 } 225 } 226 227 public static int tableSwitchInstruction(int v0) { 228 switch (v0) { 229 case 0: 230 return 1; 231 case 1: 232 return 2; 233 case 2: 234 return 3; 235 default: 236 return -1; 237 } 238 } 239 240 public static String monitorInstructions(Object v0) { 241 synchronized (v0) { 242 return v0.toString(); 243 } 244 } 245 246 public int dupX1Instruction() { 247 return f++; 248 } 249 250 public long dup2X1Instruction() { 251 return g++; 252 } 253 254 public void dupX2Instruction(int[] v0, int[] v1) { 255 v0[0] = v1[0] = 0; 256 } 257 258 public void dup2X2Instruction(long[] v0, long[] v1) { 259 v0[0] = v1[0] = 0; 260 } 261 262 public void popInstructions() { 263 dupX1Instruction(); 264 dup2X1Instruction(); 265 } 266 267 // With JDK1.5, this code no longer produces jsr or ret instructions. 268 public int jsrAndRetInstructions(int v0) throws Exception { 269 int u0 = v0 + 1; 270 try { 271 u0 = jsrAndRetInstructions(u0); 272 } catch (Throwable t) { 273 return -1; 274 } finally { 275 u0++; 276 } 277 return u0; 278 } 279 280 public Object readNullArray() { 281 Object[] array = null; 282 try { 283 return array[0]; 284 } catch (NullPointerException e) { 285 return null; 286 } 287 } 288 } 289