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 jdk3; 29 30 /** 31 * Class which, compiled with the JDK 1.3.0, produces all the JVM instructions from that version, in 32 * particular jsr and ret (except nop and swap; wide instructions are included in LargeMethod). Must 33 * be compiled with "javac -g". 34 */ 35 class AllInstructions { 36 private int f; 37 private long g; 38 private AllInstructions field; 39 private static AllInstructions staticField; 40 AllInstructions()41 AllInstructions() {} 42 AllInstructions(int v0, float v1, long v2, double v3, Object v4)43 AllInstructions(int v0, float v1, long v2, double v3, Object v4) {} 44 intInstructions( int v0, int v1, int v2, int v3, int v4, int v5, int v6, int v7, int v8)45 public static int intInstructions( 46 int v0, int v1, int v2, int v3, int v4, int v5, int v6, int v7, int v8) { 47 boolean b0 = v0 < -1; 48 boolean b1 = v1 > 1; 49 boolean b2 = v2 <= 2; 50 boolean b3 = v3 >= 3; 51 boolean b4 = v4 == 4; 52 boolean b5 = v5 != 5; 53 v0 = b0 ? (v6 + 5) : (v6 - 5); 54 v1 = b1 ? (v7 * 100) : (v7 / 100); 55 v2 = b2 ? (v8 % 10000) : (~v8); 56 v3 = b3 ? (v0 & 1000000) : (v0 | 1000000); 57 v4 = b4 ? (v1 ^ v2) : (v1 << v2); 58 v5 = b5 ? (v2 >> v3) : (v2 >>> v3); 59 v6 += 1; 60 v7 = v6 < 0 ? -v6 : v6; 61 v1 = v0 < 0 ? v1 : v2; 62 v2 = v1 > 0 ? v2 : v3; 63 v3 = v2 <= 0 ? v3 : v4; 64 v4 = v3 >= 0 ? v4 : v5; 65 v5 = v4 == 0 ? v5 : v6; 66 v6 = v5 != 0 ? v6 : v7; 67 return v0 + v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8; 68 } 69 70 public static long longInstructions( 71 long v0, long v1, long v2, long v3, long v4, long v5, long v6, long v7, long v8) { 72 boolean b0 = v0 < -1L; 73 boolean b1 = v1 > 1L; 74 boolean b2 = v2 <= 2L; 75 boolean b3 = v3 >= 3L; 76 boolean b4 = v4 == 4L; 77 boolean b5 = v5 != 5L; 78 v0 = b0 ? (v6 + 5L) : (v6 - 5L); 79 v1 = b1 ? (v7 * 100L) : (v7 / 100L); 80 v2 = b2 ? (v8 % 10000L) : (~v8); 81 v3 = b3 ? (v0 & 1000000L) : (v0 | 1000000L); 82 v4 = b4 ? (v1 ^ v2) : (v1 << v2); 83 v5 = b5 ? (v2 >> v3) : (v2 >>> v3); 84 v6 += 1L; 85 v7 = v6 < 0L ? -v6 : v6; 86 return v0 + v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8; 87 } 88 89 public static float floatInstructions( 90 float v0, float v1, float v2, float v3, float v4, float v5, float v6, float v7, float v8) { 91 boolean b0 = v0 < -1f; 92 boolean b1 = v1 > 1f; 93 boolean b2 = v2 <= 2f; 94 boolean b3 = v3 >= 3f; 95 boolean b4 = v4 == 4f; 96 boolean b5 = v5 != 5f; 97 v0 = b0 ? (v6 + 5f) : (v6 - 5f); 98 v1 = b1 ? (v7 * 100f) : (v7 / 100f); 99 v2 = b2 ? (v8 % 10000f) : v8; 100 v3 = b3 ? -v3 : v3; 101 v4 = b4 ? -v4 : v4; 102 v5 = b5 ? -v5 : v5; 103 v6 += 1f; 104 v7 = v6 < 0f ? -v6 : v6; 105 v8 = v7; 106 return v0 + v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8; 107 } 108 109 public static double doubleInstructions( 110 double v0, 111 double v1, 112 double v2, 113 double v3, 114 double v4, 115 double v5, 116 double v6, 117 double v7, 118 double v8) { 119 boolean b0 = v0 < -1d; 120 boolean b1 = v1 > 1d; 121 boolean b2 = v2 <= 2d; 122 boolean b3 = v3 >= 3d; 123 boolean b4 = v4 == 4d; 124 boolean b5 = v5 != 5d; 125 v0 = b0 ? (v6 + 5d) : (v6 - 5d); 126 v1 = b1 ? (v7 * 100d) : (v7 / 100d); 127 v2 = b2 ? (v8 % 10000d) : v8; 128 v3 = b3 ? -v3 : v3; 129 v4 = b4 ? -v4 : v4; 130 v5 = b5 ? -v5 : v5; 131 v6 += 1d; 132 v7 = v6 < 0d ? -v6 : v6; 133 return v0 + v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8; 134 } 135 136 public static double castInstructions(int v0, long v1, long v2) { 137 byte v3 = (byte) v0; 138 char v4 = (char) v1; 139 short v5 = (short) v2; 140 long v6 = (long) v3; 141 float v7 = (float) v4; 142 double v8 = (double) v5; 143 v1 = v6; 144 v2 = v1; 145 v6 = (long) v8; 146 return v0 + v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8; 147 } 148 149 public static float castInstructions(float v0, double v1, double v2) { 150 byte v3 = (byte) v0; 151 char v4 = (char) v1; 152 short v5 = (short) v2; 153 long v6 = (long) v3; 154 float v7 = (float) v4; 155 double v8 = (double) v5; 156 v1 = v6; 157 v2 = v1; 158 v6 = (long) v7; 159 return (float) (v0 + v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8); 160 } 161 162 public static Object objectInstructions(Object v0, Object v1, Object v2, Object v3, Object v4) { 163 boolean b0 = v0 == v1; 164 boolean b1 = v1 != v2; 165 boolean b2 = v2 == null; 166 boolean b3 = v3 != null; 167 boolean b4 = v4 instanceof String; 168 v0 = b0 ? null : v0; 169 v1 = b1 ? v1 : v0; 170 v2 = b2 ? v2 : v1; 171 v3 = b3 ? v3 : v2; 172 v4 = b4 ? new Integer(((String) v4).length()) : v3; 173 return v4; 174 } 175 176 public static Object[] arrayInstructions( 177 byte[] v0, char[] v1, short[] v2, int[] v3, long[] v4, float[] v5, double[] v6, Object[] v7) { 178 v0[1] = v0[0]; 179 v1[1] = v1[0]; 180 v2[1] = v2[0]; 181 v3[1] = v3[0]; 182 v4[1] = v4[0]; 183 v5[1] = v5[0]; 184 v6[1] = v6[0]; 185 v7[1] = v7[0]; 186 Object[] v8 = new Object[v7.length]; 187 v8[0] = new int[4][8][16]; 188 return v8; 189 } 190 191 public void fieldInstructions() { 192 AllInstructions c = field; 193 field = staticField; 194 staticField = c; 195 } 196 197 public void methodInstructions(Runnable v0) { 198 AllInstructions c = new AllInstructions(); 199 c.fieldInstructions(); 200 monitorInstructions(c); 201 v0.run(); 202 } 203 204 public static int lookupSwitchInstruction(int v0) { 205 switch (v0) { 206 case 1000: 207 return 1; 208 case 10000: 209 return 2; 210 case 100000: 211 return 3; 212 default: 213 return -1; 214 } 215 } 216 217 public static int tableSwitchInstruction(int v0) { 218 switch (v0) { 219 case 0: 220 return 1; 221 case 1: 222 return 2; 223 case 2: 224 return 3; 225 default: 226 return -1; 227 } 228 } 229 230 public static String monitorInstructions(Object v0) { 231 synchronized (v0) { 232 return v0.toString(); 233 } 234 } 235 236 public int dupX1Instruction() { 237 return f++; 238 } 239 240 public void dup2Instruction(long[] v0, int i, int j) { 241 v0[i] |= (1L << j); 242 } 243 244 public long dup2X1Instruction() { 245 return g++; 246 } 247 248 public void dup2X1InstructionVariant(String[] v0, int i, Object o) { 249 v0[i] += " " + o.toString(); 250 } 251 252 public void dupX2Instruction(int[] v0, int[] v1) { 253 v0[0] = v1[0] = 0; 254 } 255 256 public void dup2X2Instruction(long[] v0, long[] v1) { 257 v0[0] = v1[0] = 0; 258 } 259 260 public void popInstructions() { 261 dupX1Instruction(); 262 dup2X1Instruction(); 263 } 264 265 public int jsrAndRetInstructions(int v0) throws Exception { 266 int u0 = v0 + 1; 267 try { 268 u0 = jsrAndRetInstructions(u0); 269 } catch (Throwable t) { 270 return -1; 271 } finally { 272 u0++; 273 } 274 return u0; 275 } 276 277 public Object readNullArray() { 278 Object[] array = null; 279 try { 280 return array[0]; 281 } catch (NullPointerException e) { 282 return null; 283 } 284 } 285 } 286