annotate src/clojure/asm/commons/GeneratorAdapter.java @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
rev   line source
rlm@10 1 /***
rlm@10 2 * ASM: a very small and fast Java bytecode manipulation framework
rlm@10 3 * Copyright (c) 2000-2005 INRIA, France Telecom
rlm@10 4 * All rights reserved.
rlm@10 5 *
rlm@10 6 * Redistribution and use in source and binary forms, with or without
rlm@10 7 * modification, are permitted provided that the following conditions
rlm@10 8 * are met:
rlm@10 9 * 1. Redistributions of source code must retain the above copyright
rlm@10 10 * notice, this list of conditions and the following disclaimer.
rlm@10 11 * 2. Redistributions in binary form must reproduce the above copyright
rlm@10 12 * notice, this list of conditions and the following disclaimer in the
rlm@10 13 * documentation and/or other materials provided with the distribution.
rlm@10 14 * 3. Neither the name of the copyright holders nor the names of its
rlm@10 15 * contributors may be used to endorse or promote products derived from
rlm@10 16 * this software without specific prior written permission.
rlm@10 17 *
rlm@10 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
rlm@10 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
rlm@10 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
rlm@10 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
rlm@10 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
rlm@10 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
rlm@10 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
rlm@10 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
rlm@10 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
rlm@10 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
rlm@10 28 * THE POSSIBILITY OF SUCH DAMAGE.
rlm@10 29 */
rlm@10 30 package clojure.asm.commons;
rlm@10 31
rlm@10 32 import java.util.ArrayList;
rlm@10 33 import java.util.Arrays;
rlm@10 34 import java.util.List;
rlm@10 35
rlm@10 36 import clojure.asm.ClassVisitor;
rlm@10 37 import clojure.asm.Label;
rlm@10 38 import clojure.asm.MethodVisitor;
rlm@10 39 import clojure.asm.Opcodes;
rlm@10 40 import clojure.asm.Type;
rlm@10 41
rlm@10 42 /**
rlm@10 43 * A {@link clojure.asm.MethodAdapter} with convenient methods to generate
rlm@10 44 * code. For example, using this adapter, the class below
rlm@10 45 * <p/>
rlm@10 46 * <pre>
rlm@10 47 * public class Example {
rlm@10 48 * public static void main(String[] args) {
rlm@10 49 * System.out.println(&quot;Hello world!&quot;);
rlm@10 50 * }
rlm@10 51 * }
rlm@10 52 * </pre>
rlm@10 53 * <p/>
rlm@10 54 * can be generated as follows:
rlm@10 55 * <p/>
rlm@10 56 * <pre>
rlm@10 57 * ClassWriter cw = new ClassWriter(true);
rlm@10 58 * cw.visit(V1_1, ACC_PUBLIC, &quot;Example&quot;, null, &quot;java/lang/Object&quot;, null);
rlm@10 59 * <p/>
rlm@10 60 * Method m = Method.getMethod(&quot;void &lt;init&gt; ()&quot;);
rlm@10 61 * GeneratorAdapter mg = new GeneratorAdapter(ACC_PUBLIC, m, null, null, cw);
rlm@10 62 * mg.loadThis();
rlm@10 63 * mg.invokeConstructor(Type.getType(Object.class), m);
rlm@10 64 * mg.returnValue();
rlm@10 65 * mg.endMethod();
rlm@10 66 * <p/>
rlm@10 67 * m = Method.getMethod(&quot;void main (String[])&quot;);
rlm@10 68 * mg = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC, m, null, null, cw);
rlm@10 69 * mg.getStatic(Type.getType(System.class), &quot;out&quot;, Type.getType(PrintStream.class));
rlm@10 70 * mg.push(&quot;Hello world!&quot;);
rlm@10 71 * mg.invokeVirtual(Type.getType(PrintStream.class), Method.getMethod(&quot;void println (String)&quot;));
rlm@10 72 * mg.returnValue();
rlm@10 73 * mg.endMethod();
rlm@10 74 * <p/>
rlm@10 75 * cw.visitEnd();
rlm@10 76 * </pre>
rlm@10 77 *
rlm@10 78 * @author Juozas Baliuka
rlm@10 79 * @author Chris Nokleberg
rlm@10 80 * @author Eric Bruneton
rlm@10 81 */
rlm@10 82 public class GeneratorAdapter extends LocalVariablesSorter{
rlm@10 83
rlm@10 84 private final static Type BYTE_TYPE = Type.getObjectType("java/lang/Byte");
rlm@10 85
rlm@10 86 private final static Type BOOLEAN_TYPE = Type.getObjectType("java/lang/Boolean");
rlm@10 87
rlm@10 88 private final static Type SHORT_TYPE = Type.getObjectType("java/lang/Short");
rlm@10 89
rlm@10 90 private final static Type CHARACTER_TYPE = Type.getObjectType("java/lang/Character");
rlm@10 91
rlm@10 92 private final static Type INTEGER_TYPE = Type.getObjectType("java/lang/Integer");
rlm@10 93
rlm@10 94 private final static Type FLOAT_TYPE = Type.getObjectType("java/lang/Float");
rlm@10 95
rlm@10 96 private final static Type LONG_TYPE = Type.getObjectType("java/lang/Long");
rlm@10 97
rlm@10 98 private final static Type DOUBLE_TYPE = Type.getObjectType("java/lang/Double");
rlm@10 99
rlm@10 100 private final static Type NUMBER_TYPE = Type.getObjectType("java/lang/Number");
rlm@10 101
rlm@10 102 private final static Type OBJECT_TYPE = Type.getObjectType("java/lang/Object");
rlm@10 103
rlm@10 104 private final static Method BOOLEAN_VALUE = Method.getMethod("boolean booleanValue()");
rlm@10 105
rlm@10 106 private final static Method CHAR_VALUE = Method.getMethod("char charValue()");
rlm@10 107
rlm@10 108 private final static Method INT_VALUE = Method.getMethod("int intValue()");
rlm@10 109
rlm@10 110 private final static Method FLOAT_VALUE = Method.getMethod("float floatValue()");
rlm@10 111
rlm@10 112 private final static Method LONG_VALUE = Method.getMethod("long longValue()");
rlm@10 113
rlm@10 114 private final static Method DOUBLE_VALUE = Method.getMethod("double doubleValue()");
rlm@10 115
rlm@10 116 /**
rlm@10 117 * Constant for the {@link #math math} method.
rlm@10 118 */
rlm@10 119 public final static int ADD = Opcodes.IADD;
rlm@10 120
rlm@10 121 /**
rlm@10 122 * Constant for the {@link #math math} method.
rlm@10 123 */
rlm@10 124 public final static int SUB = Opcodes.ISUB;
rlm@10 125
rlm@10 126 /**
rlm@10 127 * Constant for the {@link #math math} method.
rlm@10 128 */
rlm@10 129 public final static int MUL = Opcodes.IMUL;
rlm@10 130
rlm@10 131 /**
rlm@10 132 * Constant for the {@link #math math} method.
rlm@10 133 */
rlm@10 134 public final static int DIV = Opcodes.IDIV;
rlm@10 135
rlm@10 136 /**
rlm@10 137 * Constant for the {@link #math math} method.
rlm@10 138 */
rlm@10 139 public final static int REM = Opcodes.IREM;
rlm@10 140
rlm@10 141 /**
rlm@10 142 * Constant for the {@link #math math} method.
rlm@10 143 */
rlm@10 144 public final static int NEG = Opcodes.INEG;
rlm@10 145
rlm@10 146 /**
rlm@10 147 * Constant for the {@link #math math} method.
rlm@10 148 */
rlm@10 149 public final static int SHL = Opcodes.ISHL;
rlm@10 150
rlm@10 151 /**
rlm@10 152 * Constant for the {@link #math math} method.
rlm@10 153 */
rlm@10 154 public final static int SHR = Opcodes.ISHR;
rlm@10 155
rlm@10 156 /**
rlm@10 157 * Constant for the {@link #math math} method.
rlm@10 158 */
rlm@10 159 public final static int USHR = Opcodes.IUSHR;
rlm@10 160
rlm@10 161 /**
rlm@10 162 * Constant for the {@link #math math} method.
rlm@10 163 */
rlm@10 164 public final static int AND = Opcodes.IAND;
rlm@10 165
rlm@10 166 /**
rlm@10 167 * Constant for the {@link #math math} method.
rlm@10 168 */
rlm@10 169 public final static int OR = Opcodes.IOR;
rlm@10 170
rlm@10 171 /**
rlm@10 172 * Constant for the {@link #math math} method.
rlm@10 173 */
rlm@10 174 public final static int XOR = Opcodes.IXOR;
rlm@10 175
rlm@10 176 /**
rlm@10 177 * Constant for the {@link #ifCmp ifCmp} method.
rlm@10 178 */
rlm@10 179 public final static int EQ = Opcodes.IFEQ;
rlm@10 180
rlm@10 181 /**
rlm@10 182 * Constant for the {@link #ifCmp ifCmp} method.
rlm@10 183 */
rlm@10 184 public final static int NE = Opcodes.IFNE;
rlm@10 185
rlm@10 186 /**
rlm@10 187 * Constant for the {@link #ifCmp ifCmp} method.
rlm@10 188 */
rlm@10 189 public final static int LT = Opcodes.IFLT;
rlm@10 190
rlm@10 191 /**
rlm@10 192 * Constant for the {@link #ifCmp ifCmp} method.
rlm@10 193 */
rlm@10 194 public final static int GE = Opcodes.IFGE;
rlm@10 195
rlm@10 196 /**
rlm@10 197 * Constant for the {@link #ifCmp ifCmp} method.
rlm@10 198 */
rlm@10 199 public final static int GT = Opcodes.IFGT;
rlm@10 200
rlm@10 201 /**
rlm@10 202 * Constant for the {@link #ifCmp ifCmp} method.
rlm@10 203 */
rlm@10 204 public final static int LE = Opcodes.IFLE;
rlm@10 205
rlm@10 206 /**
rlm@10 207 * Access flags of the method visited by this adapter.
rlm@10 208 */
rlm@10 209 private final int access;
rlm@10 210
rlm@10 211 /**
rlm@10 212 * Return type of the method visited by this adapter.
rlm@10 213 */
rlm@10 214 private final Type returnType;
rlm@10 215
rlm@10 216 /**
rlm@10 217 * Argument types of the method visited by this adapter.
rlm@10 218 */
rlm@10 219 private final Type[] argumentTypes;
rlm@10 220
rlm@10 221 /**
rlm@10 222 * Types of the local variables of the method visited by this adapter.
rlm@10 223 */
rlm@10 224 private final List localTypes = new ArrayList();
rlm@10 225
rlm@10 226 /**
rlm@10 227 * Creates a new {@link GeneratorAdapter}.
rlm@10 228 *
rlm@10 229 * @param mv the method visitor to which this adapter delegates calls.
rlm@10 230 * @param access the method's access flags (see {@link Opcodes}).
rlm@10 231 * @param name the method's name.
rlm@10 232 * @param desc the method's descriptor (see {@link Type Type}).
rlm@10 233 */
rlm@10 234 public GeneratorAdapter(
rlm@10 235 final MethodVisitor mv,
rlm@10 236 final int access,
rlm@10 237 final String name,
rlm@10 238 final String desc){
rlm@10 239 super(access, desc, mv);
rlm@10 240 this.access = access;
rlm@10 241 this.returnType = Type.getReturnType(desc);
rlm@10 242 this.argumentTypes = Type.getArgumentTypes(desc);
rlm@10 243 }
rlm@10 244
rlm@10 245 /**
rlm@10 246 * Creates a new {@link GeneratorAdapter}.
rlm@10 247 *
rlm@10 248 * @param access access flags of the adapted method.
rlm@10 249 * @param method the adapted method.
rlm@10 250 * @param mv the method visitor to which this adapter delegates calls.
rlm@10 251 */
rlm@10 252 public GeneratorAdapter(
rlm@10 253 final int access,
rlm@10 254 final Method method,
rlm@10 255 final MethodVisitor mv){
rlm@10 256 super(access, method.getDescriptor(), mv);
rlm@10 257 this.access = access;
rlm@10 258 this.returnType = method.getReturnType();
rlm@10 259 this.argumentTypes = method.getArgumentTypes();
rlm@10 260 }
rlm@10 261
rlm@10 262 /**
rlm@10 263 * Creates a new {@link GeneratorAdapter}.
rlm@10 264 *
rlm@10 265 * @param access access flags of the adapted method.
rlm@10 266 * @param method the adapted method.
rlm@10 267 * @param signature the signature of the adapted method (may be
rlm@10 268 * <tt>null</tt>).
rlm@10 269 * @param exceptions the exceptions thrown by the adapted method (may be
rlm@10 270 * <tt>null</tt>).
rlm@10 271 * @param cv the class visitor to which this adapter delegates calls.
rlm@10 272 */
rlm@10 273 public GeneratorAdapter(
rlm@10 274 final int access,
rlm@10 275 final Method method,
rlm@10 276 final String signature,
rlm@10 277 final Type[] exceptions,
rlm@10 278 final ClassVisitor cv){
rlm@10 279 this(access, method, cv.visitMethod(access,
rlm@10 280 method.getName(),
rlm@10 281 method.getDescriptor(),
rlm@10 282 signature,
rlm@10 283 getInternalNames(exceptions)));
rlm@10 284 }
rlm@10 285
rlm@10 286 /**
rlm@10 287 * Returns the internal names of the given types.
rlm@10 288 *
rlm@10 289 * @param types a set of types.
rlm@10 290 * @return the internal names of the given types.
rlm@10 291 */
rlm@10 292 private static String[] getInternalNames(final Type[] types){
rlm@10 293 if(types == null)
rlm@10 294 {
rlm@10 295 return null;
rlm@10 296 }
rlm@10 297 String[] names = new String[types.length];
rlm@10 298 for(int i = 0; i < names.length; ++i)
rlm@10 299 {
rlm@10 300 names[i] = types[i].getInternalName();
rlm@10 301 }
rlm@10 302 return names;
rlm@10 303 }
rlm@10 304
rlm@10 305 // ------------------------------------------------------------------------
rlm@10 306 // Instructions to push constants on the stack
rlm@10 307 // ------------------------------------------------------------------------
rlm@10 308
rlm@10 309 /**
rlm@10 310 * Generates the instruction to push the given value on the stack.
rlm@10 311 *
rlm@10 312 * @param value the value to be pushed on the stack.
rlm@10 313 */
rlm@10 314 public void push(final boolean value){
rlm@10 315 push(value ? 1 : 0);
rlm@10 316 }
rlm@10 317
rlm@10 318 /**
rlm@10 319 * Generates the instruction to push the given value on the stack.
rlm@10 320 *
rlm@10 321 * @param value the value to be pushed on the stack.
rlm@10 322 */
rlm@10 323 public void push(final int value){
rlm@10 324 if(value >= -1 && value <= 5)
rlm@10 325 {
rlm@10 326 mv.visitInsn(Opcodes.ICONST_0 + value);
rlm@10 327 }
rlm@10 328 else if(value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE)
rlm@10 329 {
rlm@10 330 mv.visitIntInsn(Opcodes.BIPUSH, value);
rlm@10 331 }
rlm@10 332 else if(value >= Short.MIN_VALUE && value <= Short.MAX_VALUE)
rlm@10 333 {
rlm@10 334 mv.visitIntInsn(Opcodes.SIPUSH, value);
rlm@10 335 }
rlm@10 336 else
rlm@10 337 {
rlm@10 338 mv.visitLdcInsn(new Integer(value));
rlm@10 339 }
rlm@10 340 }
rlm@10 341
rlm@10 342 /**
rlm@10 343 * Generates the instruction to push the given value on the stack.
rlm@10 344 *
rlm@10 345 * @param value the value to be pushed on the stack.
rlm@10 346 */
rlm@10 347 public void push(final long value){
rlm@10 348 if(value == 0L || value == 1L)
rlm@10 349 {
rlm@10 350 mv.visitInsn(Opcodes.LCONST_0 + (int) value);
rlm@10 351 }
rlm@10 352 else
rlm@10 353 {
rlm@10 354 mv.visitLdcInsn(new Long(value));
rlm@10 355 }
rlm@10 356 }
rlm@10 357
rlm@10 358 /**
rlm@10 359 * Generates the instruction to push the given value on the stack.
rlm@10 360 *
rlm@10 361 * @param value the value to be pushed on the stack.
rlm@10 362 */
rlm@10 363 public void push(final float value){
rlm@10 364 int bits = Float.floatToIntBits(value);
rlm@10 365 if(bits == 0L || bits == 0x3f800000 || bits == 0x40000000)
rlm@10 366 { // 0..2
rlm@10 367 mv.visitInsn(Opcodes.FCONST_0 + (int) value);
rlm@10 368 }
rlm@10 369 else
rlm@10 370 {
rlm@10 371 mv.visitLdcInsn(new Float(value));
rlm@10 372 }
rlm@10 373 }
rlm@10 374
rlm@10 375 /**
rlm@10 376 * Generates the instruction to push the given value on the stack.
rlm@10 377 *
rlm@10 378 * @param value the value to be pushed on the stack.
rlm@10 379 */
rlm@10 380 public void push(final double value){
rlm@10 381 long bits = Double.doubleToLongBits(value);
rlm@10 382 if(bits == 0L || bits == 0x3ff0000000000000L)
rlm@10 383 { // +0.0d and 1.0d
rlm@10 384 mv.visitInsn(Opcodes.DCONST_0 + (int) value);
rlm@10 385 }
rlm@10 386 else
rlm@10 387 {
rlm@10 388 mv.visitLdcInsn(new Double(value));
rlm@10 389 }
rlm@10 390 }
rlm@10 391
rlm@10 392 /**
rlm@10 393 * Generates the instruction to push the given value on the stack.
rlm@10 394 *
rlm@10 395 * @param value the value to be pushed on the stack. May be <tt>null</tt>.
rlm@10 396 */
rlm@10 397 public void push(final String value){
rlm@10 398 if(value == null)
rlm@10 399 {
rlm@10 400 mv.visitInsn(Opcodes.ACONST_NULL);
rlm@10 401 }
rlm@10 402 else
rlm@10 403 {
rlm@10 404 mv.visitLdcInsn(value);
rlm@10 405 }
rlm@10 406 }
rlm@10 407
rlm@10 408 /**
rlm@10 409 * Generates the instruction to push the given value on the stack.
rlm@10 410 *
rlm@10 411 * @param value the value to be pushed on the stack.
rlm@10 412 */
rlm@10 413 public void push(final Type value){
rlm@10 414 if(value == null)
rlm@10 415 {
rlm@10 416 mv.visitInsn(Opcodes.ACONST_NULL);
rlm@10 417 }
rlm@10 418 else
rlm@10 419 {
rlm@10 420 mv.visitLdcInsn(value);
rlm@10 421 }
rlm@10 422 }
rlm@10 423
rlm@10 424 // ------------------------------------------------------------------------
rlm@10 425 // Instructions to load and store method arguments
rlm@10 426 // ------------------------------------------------------------------------
rlm@10 427
rlm@10 428 /**
rlm@10 429 * Returns the index of the given method argument in the frame's local
rlm@10 430 * variables array.
rlm@10 431 *
rlm@10 432 * @param arg the index of a method argument.
rlm@10 433 * @return the index of the given method argument in the frame's local
rlm@10 434 * variables array.
rlm@10 435 */
rlm@10 436 private int getArgIndex(final int arg){
rlm@10 437 int index = (access & Opcodes.ACC_STATIC) == 0 ? 1 : 0;
rlm@10 438 for(int i = 0; i < arg; i++)
rlm@10 439 {
rlm@10 440 index += argumentTypes[i].getSize();
rlm@10 441 }
rlm@10 442 return index;
rlm@10 443 }
rlm@10 444
rlm@10 445 /**
rlm@10 446 * Generates the instruction to push a local variable on the stack.
rlm@10 447 *
rlm@10 448 * @param type the type of the local variable to be loaded.
rlm@10 449 * @param index an index in the frame's local variables array.
rlm@10 450 */
rlm@10 451 private void loadInsn(final Type type, final int index){
rlm@10 452 mv.visitVarInsn(type.getOpcode(Opcodes.ILOAD), index);
rlm@10 453 }
rlm@10 454
rlm@10 455 /**
rlm@10 456 * Generates the instruction to store the top stack value in a local
rlm@10 457 * variable.
rlm@10 458 *
rlm@10 459 * @param type the type of the local variable to be stored.
rlm@10 460 * @param index an index in the frame's local variables array.
rlm@10 461 */
rlm@10 462 private void storeInsn(final Type type, final int index){
rlm@10 463 mv.visitVarInsn(type.getOpcode(Opcodes.ISTORE), index);
rlm@10 464 }
rlm@10 465
rlm@10 466 /**
rlm@10 467 * Generates the instruction to load 'this' on the stack.
rlm@10 468 */
rlm@10 469 public void loadThis(){
rlm@10 470 if((access & Opcodes.ACC_STATIC) != 0)
rlm@10 471 {
rlm@10 472 throw new IllegalStateException("no 'this' pointer within static method");
rlm@10 473 }
rlm@10 474 mv.visitVarInsn(Opcodes.ALOAD, 0);
rlm@10 475 }
rlm@10 476
rlm@10 477 /**
rlm@10 478 * Generates the instruction to load the given method argument on the stack.
rlm@10 479 *
rlm@10 480 * @param arg the index of a method argument.
rlm@10 481 */
rlm@10 482 public void loadArg(final int arg){
rlm@10 483 loadInsn(argumentTypes[arg], getArgIndex(arg));
rlm@10 484 }
rlm@10 485
rlm@10 486 /**
rlm@10 487 * Generates the instructions to load the given method arguments on the
rlm@10 488 * stack.
rlm@10 489 *
rlm@10 490 * @param arg the index of the first method argument to be loaded.
rlm@10 491 * @param count the number of method arguments to be loaded.
rlm@10 492 */
rlm@10 493 public void loadArgs(final int arg, final int count){
rlm@10 494 int index = getArgIndex(arg);
rlm@10 495 for(int i = 0; i < count; ++i)
rlm@10 496 {
rlm@10 497 Type t = argumentTypes[arg + i];
rlm@10 498 loadInsn(t, index);
rlm@10 499 index += t.getSize();
rlm@10 500 }
rlm@10 501 }
rlm@10 502
rlm@10 503 /**
rlm@10 504 * Generates the instructions to load all the method arguments on the stack.
rlm@10 505 */
rlm@10 506 public void loadArgs(){
rlm@10 507 loadArgs(0, argumentTypes.length);
rlm@10 508 }
rlm@10 509
rlm@10 510 /**
rlm@10 511 * Generates the instructions to load all the method arguments on the stack,
rlm@10 512 * as a single object array.
rlm@10 513 */
rlm@10 514 public void loadArgArray(){
rlm@10 515 push(argumentTypes.length);
rlm@10 516 newArray(OBJECT_TYPE);
rlm@10 517 for(int i = 0; i < argumentTypes.length; i++)
rlm@10 518 {
rlm@10 519 dup();
rlm@10 520 push(i);
rlm@10 521 loadArg(i);
rlm@10 522 box(argumentTypes[i]);
rlm@10 523 arrayStore(OBJECT_TYPE);
rlm@10 524 }
rlm@10 525 }
rlm@10 526
rlm@10 527 /**
rlm@10 528 * Generates the instruction to store the top stack value in the given
rlm@10 529 * method argument.
rlm@10 530 *
rlm@10 531 * @param arg the index of a method argument.
rlm@10 532 */
rlm@10 533 public void storeArg(final int arg){
rlm@10 534 storeInsn(argumentTypes[arg], getArgIndex(arg));
rlm@10 535 }
rlm@10 536
rlm@10 537 // ------------------------------------------------------------------------
rlm@10 538 // Instructions to load and store local variables
rlm@10 539 // ------------------------------------------------------------------------
rlm@10 540
rlm@10 541 /**
rlm@10 542 * Returns the type of the given local variable.
rlm@10 543 *
rlm@10 544 * @param local a local variable identifier, as returned by
rlm@10 545 * {@link LocalVariablesSorter#newLocal(Type) newLocal()}.
rlm@10 546 * @return the type of the given local variable.
rlm@10 547 */
rlm@10 548 public Type getLocalType(final int local){
rlm@10 549 return (Type) localTypes.get(local - firstLocal);
rlm@10 550 }
rlm@10 551
rlm@10 552 protected void setLocalType(final int local, final Type type){
rlm@10 553 int index = local - firstLocal;
rlm@10 554 while(localTypes.size() < index + 1)
rlm@10 555 {
rlm@10 556 localTypes.add(null);
rlm@10 557 }
rlm@10 558 localTypes.set(index, type);
rlm@10 559 }
rlm@10 560
rlm@10 561 /**
rlm@10 562 * Generates the instruction to load the given local variable on the stack.
rlm@10 563 *
rlm@10 564 * @param local a local variable identifier, as returned by
rlm@10 565 * {@link LocalVariablesSorter#newLocal(Type) newLocal()}.
rlm@10 566 */
rlm@10 567 public void loadLocal(final int local){
rlm@10 568 loadInsn(getLocalType(local), local);
rlm@10 569 }
rlm@10 570
rlm@10 571 /**
rlm@10 572 * Generates the instruction to load the given local variable on the stack.
rlm@10 573 *
rlm@10 574 * @param local a local variable identifier, as returned by
rlm@10 575 * {@link LocalVariablesSorter#newLocal(Type) newLocal()}.
rlm@10 576 * @param type the type of this local variable.
rlm@10 577 */
rlm@10 578 public void loadLocal(final int local, final Type type){
rlm@10 579 setLocalType(local, type);
rlm@10 580 loadInsn(type, local);
rlm@10 581 }
rlm@10 582
rlm@10 583 /**
rlm@10 584 * Generates the instruction to store the top stack value in the given local
rlm@10 585 * variable.
rlm@10 586 *
rlm@10 587 * @param local a local variable identifier, as returned by
rlm@10 588 * {@link LocalVariablesSorter#newLocal(Type) newLocal()}.
rlm@10 589 */
rlm@10 590 public void storeLocal(final int local){
rlm@10 591 storeInsn(getLocalType(local), local);
rlm@10 592 }
rlm@10 593
rlm@10 594 /**
rlm@10 595 * Generates the instruction to store the top stack value in the given local
rlm@10 596 * variable.
rlm@10 597 *
rlm@10 598 * @param local a local variable identifier, as returned by
rlm@10 599 * {@link LocalVariablesSorter#newLocal(Type) newLocal()}.
rlm@10 600 * @param type the type of this local variable.
rlm@10 601 */
rlm@10 602 public void storeLocal(final int local, final Type type){
rlm@10 603 setLocalType(local, type);
rlm@10 604 storeInsn(type, local);
rlm@10 605 }
rlm@10 606
rlm@10 607 /**
rlm@10 608 * Generates the instruction to load an element from an array.
rlm@10 609 *
rlm@10 610 * @param type the type of the array element to be loaded.
rlm@10 611 */
rlm@10 612 public void arrayLoad(final Type type){
rlm@10 613 mv.visitInsn(type.getOpcode(Opcodes.IALOAD));
rlm@10 614 }
rlm@10 615
rlm@10 616 /**
rlm@10 617 * Generates the instruction to store an element in an array.
rlm@10 618 *
rlm@10 619 * @param type the type of the array element to be stored.
rlm@10 620 */
rlm@10 621 public void arrayStore(final Type type){
rlm@10 622 mv.visitInsn(type.getOpcode(Opcodes.IASTORE));
rlm@10 623 }
rlm@10 624
rlm@10 625 // ------------------------------------------------------------------------
rlm@10 626 // Instructions to manage the stack
rlm@10 627 // ------------------------------------------------------------------------
rlm@10 628
rlm@10 629 /**
rlm@10 630 * Generates a POP instruction.
rlm@10 631 */
rlm@10 632 public void pop(){
rlm@10 633 mv.visitInsn(Opcodes.POP);
rlm@10 634 }
rlm@10 635
rlm@10 636 /**
rlm@10 637 * Generates a POP2 instruction.
rlm@10 638 */
rlm@10 639 public void pop2(){
rlm@10 640 mv.visitInsn(Opcodes.POP2);
rlm@10 641 }
rlm@10 642
rlm@10 643 /**
rlm@10 644 * Generates a DUP instruction.
rlm@10 645 */
rlm@10 646 public void dup(){
rlm@10 647 mv.visitInsn(Opcodes.DUP);
rlm@10 648 }
rlm@10 649
rlm@10 650 /**
rlm@10 651 * Generates a DUP2 instruction.
rlm@10 652 */
rlm@10 653 public void dup2(){
rlm@10 654 mv.visitInsn(Opcodes.DUP2);
rlm@10 655 }
rlm@10 656
rlm@10 657 /**
rlm@10 658 * Generates a DUP_X1 instruction.
rlm@10 659 */
rlm@10 660 public void dupX1(){
rlm@10 661 mv.visitInsn(Opcodes.DUP_X1);
rlm@10 662 }
rlm@10 663
rlm@10 664 /**
rlm@10 665 * Generates a DUP_X2 instruction.
rlm@10 666 */
rlm@10 667 public void dupX2(){
rlm@10 668 mv.visitInsn(Opcodes.DUP_X2);
rlm@10 669 }
rlm@10 670
rlm@10 671 /**
rlm@10 672 * Generates a DUP2_X1 instruction.
rlm@10 673 */
rlm@10 674 public void dup2X1(){
rlm@10 675 mv.visitInsn(Opcodes.DUP2_X1);
rlm@10 676 }
rlm@10 677
rlm@10 678 /**
rlm@10 679 * Generates a DUP2_X2 instruction.
rlm@10 680 */
rlm@10 681 public void dup2X2(){
rlm@10 682 mv.visitInsn(Opcodes.DUP2_X2);
rlm@10 683 }
rlm@10 684
rlm@10 685 /**
rlm@10 686 * Generates a SWAP instruction.
rlm@10 687 */
rlm@10 688 public void swap(){
rlm@10 689 mv.visitInsn(Opcodes.SWAP);
rlm@10 690 }
rlm@10 691
rlm@10 692 /**
rlm@10 693 * Generates the instructions to swap the top two stack values.
rlm@10 694 *
rlm@10 695 * @param prev type of the top - 1 stack value.
rlm@10 696 * @param type type of the top stack value.
rlm@10 697 */
rlm@10 698 public void swap(final Type prev, final Type type){
rlm@10 699 if(type.getSize() == 1)
rlm@10 700 {
rlm@10 701 if(prev.getSize() == 1)
rlm@10 702 {
rlm@10 703 swap(); // same as dupX1(), pop();
rlm@10 704 }
rlm@10 705 else
rlm@10 706 {
rlm@10 707 dupX2();
rlm@10 708 pop();
rlm@10 709 }
rlm@10 710 }
rlm@10 711 else
rlm@10 712 {
rlm@10 713 if(prev.getSize() == 1)
rlm@10 714 {
rlm@10 715 dup2X1();
rlm@10 716 pop2();
rlm@10 717 }
rlm@10 718 else
rlm@10 719 {
rlm@10 720 dup2X2();
rlm@10 721 pop2();
rlm@10 722 }
rlm@10 723 }
rlm@10 724 }
rlm@10 725
rlm@10 726 // ------------------------------------------------------------------------
rlm@10 727 // Instructions to do mathematical and logical operations
rlm@10 728 // ------------------------------------------------------------------------
rlm@10 729
rlm@10 730 /**
rlm@10 731 * Generates the instruction to do the specified mathematical or logical
rlm@10 732 * operation.
rlm@10 733 *
rlm@10 734 * @param op a mathematical or logical operation. Must be one of ADD, SUB,
rlm@10 735 * MUL, DIV, REM, NEG, SHL, SHR, USHR, AND, OR, XOR.
rlm@10 736 * @param type the type of the operand(s) for this operation.
rlm@10 737 */
rlm@10 738 public void math(final int op, final Type type){
rlm@10 739 mv.visitInsn(type.getOpcode(op));
rlm@10 740 }
rlm@10 741
rlm@10 742 /**
rlm@10 743 * Generates the instructions to compute the bitwise negation of the top
rlm@10 744 * stack value.
rlm@10 745 */
rlm@10 746 public void not(){
rlm@10 747 mv.visitInsn(Opcodes.ICONST_1);
rlm@10 748 mv.visitInsn(Opcodes.IXOR);
rlm@10 749 }
rlm@10 750
rlm@10 751 /**
rlm@10 752 * Generates the instruction to increment the given local variable.
rlm@10 753 *
rlm@10 754 * @param local the local variable to be incremented.
rlm@10 755 * @param amount the amount by which the local variable must be incremented.
rlm@10 756 */
rlm@10 757 public void iinc(final int local, final int amount){
rlm@10 758 mv.visitIincInsn(local, amount);
rlm@10 759 }
rlm@10 760
rlm@10 761 /**
rlm@10 762 * Generates the instructions to cast a numerical value from one type to
rlm@10 763 * another.
rlm@10 764 *
rlm@10 765 * @param from the type of the top stack value
rlm@10 766 * @param to the type into which this value must be cast.
rlm@10 767 */
rlm@10 768 public void cast(final Type from, final Type to){
rlm@10 769 if(from != to)
rlm@10 770 {
rlm@10 771 if(from == Type.DOUBLE_TYPE)
rlm@10 772 {
rlm@10 773 if(to == Type.FLOAT_TYPE)
rlm@10 774 {
rlm@10 775 mv.visitInsn(Opcodes.D2F);
rlm@10 776 }
rlm@10 777 else if(to == Type.LONG_TYPE)
rlm@10 778 {
rlm@10 779 mv.visitInsn(Opcodes.D2L);
rlm@10 780 }
rlm@10 781 else
rlm@10 782 {
rlm@10 783 mv.visitInsn(Opcodes.D2I);
rlm@10 784 cast(Type.INT_TYPE, to);
rlm@10 785 }
rlm@10 786 }
rlm@10 787 else if(from == Type.FLOAT_TYPE)
rlm@10 788 {
rlm@10 789 if(to == Type.DOUBLE_TYPE)
rlm@10 790 {
rlm@10 791 mv.visitInsn(Opcodes.F2D);
rlm@10 792 }
rlm@10 793 else if(to == Type.LONG_TYPE)
rlm@10 794 {
rlm@10 795 mv.visitInsn(Opcodes.F2L);
rlm@10 796 }
rlm@10 797 else
rlm@10 798 {
rlm@10 799 mv.visitInsn(Opcodes.F2I);
rlm@10 800 cast(Type.INT_TYPE, to);
rlm@10 801 }
rlm@10 802 }
rlm@10 803 else if(from == Type.LONG_TYPE)
rlm@10 804 {
rlm@10 805 if(to == Type.DOUBLE_TYPE)
rlm@10 806 {
rlm@10 807 mv.visitInsn(Opcodes.L2D);
rlm@10 808 }
rlm@10 809 else if(to == Type.FLOAT_TYPE)
rlm@10 810 {
rlm@10 811 mv.visitInsn(Opcodes.L2F);
rlm@10 812 }
rlm@10 813 else
rlm@10 814 {
rlm@10 815 mv.visitInsn(Opcodes.L2I);
rlm@10 816 cast(Type.INT_TYPE, to);
rlm@10 817 }
rlm@10 818 }
rlm@10 819 else
rlm@10 820 {
rlm@10 821 if(to == Type.BYTE_TYPE)
rlm@10 822 {
rlm@10 823 mv.visitInsn(Opcodes.I2B);
rlm@10 824 }
rlm@10 825 else if(to == Type.CHAR_TYPE)
rlm@10 826 {
rlm@10 827 mv.visitInsn(Opcodes.I2C);
rlm@10 828 }
rlm@10 829 else if(to == Type.DOUBLE_TYPE)
rlm@10 830 {
rlm@10 831 mv.visitInsn(Opcodes.I2D);
rlm@10 832 }
rlm@10 833 else if(to == Type.FLOAT_TYPE)
rlm@10 834 {
rlm@10 835 mv.visitInsn(Opcodes.I2F);
rlm@10 836 }
rlm@10 837 else if(to == Type.LONG_TYPE)
rlm@10 838 {
rlm@10 839 mv.visitInsn(Opcodes.I2L);
rlm@10 840 }
rlm@10 841 else if(to == Type.SHORT_TYPE)
rlm@10 842 {
rlm@10 843 mv.visitInsn(Opcodes.I2S);
rlm@10 844 }
rlm@10 845 }
rlm@10 846 }
rlm@10 847 }
rlm@10 848
rlm@10 849 // ------------------------------------------------------------------------
rlm@10 850 // Instructions to do boxing and unboxing operations
rlm@10 851 // ------------------------------------------------------------------------
rlm@10 852
rlm@10 853 /**
rlm@10 854 * Generates the instructions to box the top stack value. This value is
rlm@10 855 * replaced by its boxed equivalent on top of the stack.
rlm@10 856 *
rlm@10 857 * @param type the type of the top stack value.
rlm@10 858 */
rlm@10 859 public void box(final Type type){
rlm@10 860 if(type.getSort() == Type.OBJECT || type.getSort() == Type.ARRAY)
rlm@10 861 {
rlm@10 862 return;
rlm@10 863 }
rlm@10 864 if(type == Type.VOID_TYPE)
rlm@10 865 {
rlm@10 866 push((String) null);
rlm@10 867 }
rlm@10 868 else
rlm@10 869 {
rlm@10 870 Type boxed = type;
rlm@10 871 switch(type.getSort())
rlm@10 872 {
rlm@10 873 case Type.BYTE:
rlm@10 874 boxed = BYTE_TYPE;
rlm@10 875 break;
rlm@10 876 case Type.BOOLEAN:
rlm@10 877 boxed = BOOLEAN_TYPE;
rlm@10 878 break;
rlm@10 879 case Type.SHORT:
rlm@10 880 boxed = SHORT_TYPE;
rlm@10 881 break;
rlm@10 882 case Type.CHAR:
rlm@10 883 boxed = CHARACTER_TYPE;
rlm@10 884 break;
rlm@10 885 case Type.INT:
rlm@10 886 boxed = INTEGER_TYPE;
rlm@10 887 break;
rlm@10 888 case Type.FLOAT:
rlm@10 889 boxed = FLOAT_TYPE;
rlm@10 890 break;
rlm@10 891 case Type.LONG:
rlm@10 892 boxed = LONG_TYPE;
rlm@10 893 break;
rlm@10 894 case Type.DOUBLE:
rlm@10 895 boxed = DOUBLE_TYPE;
rlm@10 896 break;
rlm@10 897 }
rlm@10 898 newInstance(boxed);
rlm@10 899 if(type.getSize() == 2)
rlm@10 900 {
rlm@10 901 // Pp -> Ppo -> oPpo -> ooPpo -> ooPp -> o
rlm@10 902 dupX2();
rlm@10 903 dupX2();
rlm@10 904 pop();
rlm@10 905 }
rlm@10 906 else
rlm@10 907 {
rlm@10 908 // p -> po -> opo -> oop -> o
rlm@10 909 dupX1();
rlm@10 910 swap();
rlm@10 911 }
rlm@10 912 invokeConstructor(boxed, new Method("<init>",
rlm@10 913 Type.VOID_TYPE,
rlm@10 914 new Type[]{type}));
rlm@10 915 }
rlm@10 916 }
rlm@10 917
rlm@10 918 /**
rlm@10 919 * Generates the instructions to unbox the top stack value. This value is
rlm@10 920 * replaced by its unboxed equivalent on top of the stack.
rlm@10 921 *
rlm@10 922 * @param type the type of the top stack value.
rlm@10 923 */
rlm@10 924 public void unbox(final Type type){
rlm@10 925 Type t = NUMBER_TYPE;
rlm@10 926 Method sig = null;
rlm@10 927 switch(type.getSort())
rlm@10 928 {
rlm@10 929 case Type.VOID:
rlm@10 930 return;
rlm@10 931 case Type.CHAR:
rlm@10 932 t = CHARACTER_TYPE;
rlm@10 933 sig = CHAR_VALUE;
rlm@10 934 break;
rlm@10 935 case Type.BOOLEAN:
rlm@10 936 t = BOOLEAN_TYPE;
rlm@10 937 sig = BOOLEAN_VALUE;
rlm@10 938 break;
rlm@10 939 case Type.DOUBLE:
rlm@10 940 sig = DOUBLE_VALUE;
rlm@10 941 break;
rlm@10 942 case Type.FLOAT:
rlm@10 943 sig = FLOAT_VALUE;
rlm@10 944 break;
rlm@10 945 case Type.LONG:
rlm@10 946 sig = LONG_VALUE;
rlm@10 947 break;
rlm@10 948 case Type.INT:
rlm@10 949 case Type.SHORT:
rlm@10 950 case Type.BYTE:
rlm@10 951 sig = INT_VALUE;
rlm@10 952 }
rlm@10 953 if(sig == null)
rlm@10 954 {
rlm@10 955 checkCast(type);
rlm@10 956 }
rlm@10 957 else
rlm@10 958 {
rlm@10 959 checkCast(t);
rlm@10 960 invokeVirtual(t, sig);
rlm@10 961 }
rlm@10 962 }
rlm@10 963
rlm@10 964 // ------------------------------------------------------------------------
rlm@10 965 // Instructions to jump to other instructions
rlm@10 966 // ------------------------------------------------------------------------
rlm@10 967
rlm@10 968 /**
rlm@10 969 * Creates a new {@link Label}.
rlm@10 970 *
rlm@10 971 * @return a new {@link Label}.
rlm@10 972 */
rlm@10 973 public Label newLabel(){
rlm@10 974 return new Label();
rlm@10 975 }
rlm@10 976
rlm@10 977 /**
rlm@10 978 * Marks the current code position with the given label.
rlm@10 979 *
rlm@10 980 * @param label a label.
rlm@10 981 */
rlm@10 982 public void mark(final Label label){
rlm@10 983 mv.visitLabel(label);
rlm@10 984 }
rlm@10 985
rlm@10 986 /**
rlm@10 987 * Marks the current code position with a new label.
rlm@10 988 *
rlm@10 989 * @return the label that was created to mark the current code position.
rlm@10 990 */
rlm@10 991 public Label mark(){
rlm@10 992 Label label = new Label();
rlm@10 993 mv.visitLabel(label);
rlm@10 994 return label;
rlm@10 995 }
rlm@10 996
rlm@10 997 /**
rlm@10 998 * Generates the instructions to jump to a label based on the comparison of
rlm@10 999 * the top two stack values.
rlm@10 1000 *
rlm@10 1001 * @param type the type of the top two stack values.
rlm@10 1002 * @param mode how these values must be compared. One of EQ, NE, LT, GE, GT,
rlm@10 1003 * LE.
rlm@10 1004 * @param label where to jump if the comparison result is <tt>true</tt>.
rlm@10 1005 */
rlm@10 1006 public void ifCmp(final Type type, final int mode, final Label label){
rlm@10 1007 int intOp = -1;
rlm@10 1008 switch(type.getSort())
rlm@10 1009 {
rlm@10 1010 case Type.LONG:
rlm@10 1011 mv.visitInsn(Opcodes.LCMP);
rlm@10 1012 break;
rlm@10 1013 case Type.DOUBLE:
rlm@10 1014 mv.visitInsn(Opcodes.DCMPG);
rlm@10 1015 break;
rlm@10 1016 case Type.FLOAT:
rlm@10 1017 mv.visitInsn(Opcodes.FCMPG);
rlm@10 1018 break;
rlm@10 1019 case Type.ARRAY:
rlm@10 1020 case Type.OBJECT:
rlm@10 1021 switch(mode)
rlm@10 1022 {
rlm@10 1023 case EQ:
rlm@10 1024 mv.visitJumpInsn(Opcodes.IF_ACMPEQ, label);
rlm@10 1025 return;
rlm@10 1026 case NE:
rlm@10 1027 mv.visitJumpInsn(Opcodes.IF_ACMPNE, label);
rlm@10 1028 return;
rlm@10 1029 }
rlm@10 1030 throw new IllegalArgumentException("Bad comparison for type "
rlm@10 1031 + type);
rlm@10 1032 default:
rlm@10 1033 switch(mode)
rlm@10 1034 {
rlm@10 1035 case EQ:
rlm@10 1036 intOp = Opcodes.IF_ICMPEQ;
rlm@10 1037 break;
rlm@10 1038 case NE:
rlm@10 1039 intOp = Opcodes.IF_ICMPNE;
rlm@10 1040 break;
rlm@10 1041 case GE:
rlm@10 1042 intOp = Opcodes.IF_ICMPGE;
rlm@10 1043 break;
rlm@10 1044 case LT:
rlm@10 1045 intOp = Opcodes.IF_ICMPLT;
rlm@10 1046 break;
rlm@10 1047 case LE:
rlm@10 1048 intOp = Opcodes.IF_ICMPLE;
rlm@10 1049 break;
rlm@10 1050 case GT:
rlm@10 1051 intOp = Opcodes.IF_ICMPGT;
rlm@10 1052 break;
rlm@10 1053 }
rlm@10 1054 mv.visitJumpInsn(intOp, label);
rlm@10 1055 return;
rlm@10 1056 }
rlm@10 1057 int jumpMode = mode;
rlm@10 1058 switch(mode)
rlm@10 1059 {
rlm@10 1060 case GE:
rlm@10 1061 jumpMode = LT;
rlm@10 1062 break;
rlm@10 1063 case LE:
rlm@10 1064 jumpMode = GT;
rlm@10 1065 break;
rlm@10 1066 }
rlm@10 1067 mv.visitJumpInsn(jumpMode, label);
rlm@10 1068 }
rlm@10 1069
rlm@10 1070 /**
rlm@10 1071 * Generates the instructions to jump to a label based on the comparison of
rlm@10 1072 * the top two integer stack values.
rlm@10 1073 *
rlm@10 1074 * @param mode how these values must be compared. One of EQ, NE, LT, GE, GT,
rlm@10 1075 * LE.
rlm@10 1076 * @param label where to jump if the comparison result is <tt>true</tt>.
rlm@10 1077 */
rlm@10 1078 public void ifICmp(final int mode, final Label label){
rlm@10 1079 ifCmp(Type.INT_TYPE, mode, label);
rlm@10 1080 }
rlm@10 1081
rlm@10 1082 /**
rlm@10 1083 * Generates the instructions to jump to a label based on the comparison of
rlm@10 1084 * the top integer stack value with zero.
rlm@10 1085 *
rlm@10 1086 * @param mode how these values must be compared. One of EQ, NE, LT, GE, GT,
rlm@10 1087 * LE.
rlm@10 1088 * @param label where to jump if the comparison result is <tt>true</tt>.
rlm@10 1089 */
rlm@10 1090 public void ifZCmp(final int mode, final Label label){
rlm@10 1091 mv.visitJumpInsn(mode, label);
rlm@10 1092 }
rlm@10 1093
rlm@10 1094 /**
rlm@10 1095 * Generates the instruction to jump to the given label if the top stack
rlm@10 1096 * value is null.
rlm@10 1097 *
rlm@10 1098 * @param label where to jump if the condition is <tt>true</tt>.
rlm@10 1099 */
rlm@10 1100 public void ifNull(final Label label){
rlm@10 1101 mv.visitJumpInsn(Opcodes.IFNULL, label);
rlm@10 1102 }
rlm@10 1103
rlm@10 1104 /**
rlm@10 1105 * Generates the instruction to jump to the given label if the top stack
rlm@10 1106 * value is not null.
rlm@10 1107 *
rlm@10 1108 * @param label where to jump if the condition is <tt>true</tt>.
rlm@10 1109 */
rlm@10 1110 public void ifNonNull(final Label label){
rlm@10 1111 mv.visitJumpInsn(Opcodes.IFNONNULL, label);
rlm@10 1112 }
rlm@10 1113
rlm@10 1114 /**
rlm@10 1115 * Generates the instruction to jump to the given label.
rlm@10 1116 *
rlm@10 1117 * @param label where to jump if the condition is <tt>true</tt>.
rlm@10 1118 */
rlm@10 1119 public void goTo(final Label label){
rlm@10 1120 mv.visitJumpInsn(Opcodes.GOTO, label);
rlm@10 1121 }
rlm@10 1122
rlm@10 1123 /**
rlm@10 1124 * Generates a RET instruction.
rlm@10 1125 *
rlm@10 1126 * @param local a local variable identifier, as returned by
rlm@10 1127 * {@link LocalVariablesSorter#newLocal(Type) newLocal()}.
rlm@10 1128 */
rlm@10 1129 public void ret(final int local){
rlm@10 1130 mv.visitVarInsn(Opcodes.RET, local);
rlm@10 1131 }
rlm@10 1132
rlm@10 1133 /**
rlm@10 1134 * Generates the instructions for a switch statement.
rlm@10 1135 *
rlm@10 1136 * @param keys the switch case keys.
rlm@10 1137 * @param generator a generator to generate the code for the switch cases.
rlm@10 1138 */
rlm@10 1139 public void tableSwitch(
rlm@10 1140 final int[] keys,
rlm@10 1141 final TableSwitchGenerator generator){
rlm@10 1142 float density;
rlm@10 1143 if(keys.length == 0)
rlm@10 1144 {
rlm@10 1145 density = 0;
rlm@10 1146 }
rlm@10 1147 else
rlm@10 1148 {
rlm@10 1149 density = (float) keys.length
rlm@10 1150 / (keys[keys.length - 1] - keys[0] + 1);
rlm@10 1151 }
rlm@10 1152 tableSwitch(keys, generator, density >= 0.5f);
rlm@10 1153 }
rlm@10 1154
rlm@10 1155 /**
rlm@10 1156 * Generates the instructions for a switch statement.
rlm@10 1157 *
rlm@10 1158 * @param keys the switch case keys.
rlm@10 1159 * @param generator a generator to generate the code for the switch cases.
rlm@10 1160 * @param useTable <tt>true</tt> to use a TABLESWITCH instruction, or
rlm@10 1161 * <tt>false</tt> to use a LOOKUPSWITCH instruction.
rlm@10 1162 */
rlm@10 1163 public void tableSwitch(
rlm@10 1164 final int[] keys,
rlm@10 1165 final TableSwitchGenerator generator,
rlm@10 1166 final boolean useTable){
rlm@10 1167 for(int i = 1; i < keys.length; ++i)
rlm@10 1168 {
rlm@10 1169 if(keys[i] < keys[i - 1])
rlm@10 1170 {
rlm@10 1171 throw new IllegalArgumentException("keys must be sorted ascending");
rlm@10 1172 }
rlm@10 1173 }
rlm@10 1174 Label def = newLabel();
rlm@10 1175 Label end = newLabel();
rlm@10 1176 if(keys.length > 0)
rlm@10 1177 {
rlm@10 1178 int len = keys.length;
rlm@10 1179 int min = keys[0];
rlm@10 1180 int max = keys[len - 1];
rlm@10 1181 int range = max - min + 1;
rlm@10 1182 if(useTable)
rlm@10 1183 {
rlm@10 1184 Label[] labels = new Label[range];
rlm@10 1185 Arrays.fill(labels, def);
rlm@10 1186 for(int i = 0; i < len; ++i)
rlm@10 1187 {
rlm@10 1188 labels[keys[i] - min] = newLabel();
rlm@10 1189 }
rlm@10 1190 mv.visitTableSwitchInsn(min, max, def, labels);
rlm@10 1191 for(int i = 0; i < range; ++i)
rlm@10 1192 {
rlm@10 1193 Label label = labels[i];
rlm@10 1194 if(label != def)
rlm@10 1195 {
rlm@10 1196 mark(label);
rlm@10 1197 generator.generateCase(i + min, end);
rlm@10 1198 }
rlm@10 1199 }
rlm@10 1200 }
rlm@10 1201 else
rlm@10 1202 {
rlm@10 1203 Label[] labels = new Label[len];
rlm@10 1204 for(int i = 0; i < len; ++i)
rlm@10 1205 {
rlm@10 1206 labels[i] = newLabel();
rlm@10 1207 }
rlm@10 1208 mv.visitLookupSwitchInsn(def, keys, labels);
rlm@10 1209 for(int i = 0; i < len; ++i)
rlm@10 1210 {
rlm@10 1211 mark(labels[i]);
rlm@10 1212 generator.generateCase(keys[i], end);
rlm@10 1213 }
rlm@10 1214 }
rlm@10 1215 }
rlm@10 1216 mark(def);
rlm@10 1217 generator.generateDefault();
rlm@10 1218 mark(end);
rlm@10 1219 }
rlm@10 1220
rlm@10 1221 /**
rlm@10 1222 * Generates the instruction to return the top stack value to the caller.
rlm@10 1223 */
rlm@10 1224 public void returnValue(){
rlm@10 1225 mv.visitInsn(returnType.getOpcode(Opcodes.IRETURN));
rlm@10 1226 }
rlm@10 1227
rlm@10 1228 // ------------------------------------------------------------------------
rlm@10 1229 // Instructions to load and store fields
rlm@10 1230 // ------------------------------------------------------------------------
rlm@10 1231
rlm@10 1232 /**
rlm@10 1233 * Generates a get field or set field instruction.
rlm@10 1234 *
rlm@10 1235 * @param opcode the instruction's opcode.
rlm@10 1236 * @param ownerType the class in which the field is defined.
rlm@10 1237 * @param name the name of the field.
rlm@10 1238 * @param fieldType the type of the field.
rlm@10 1239 */
rlm@10 1240 private void fieldInsn(
rlm@10 1241 final int opcode,
rlm@10 1242 final Type ownerType,
rlm@10 1243 final String name,
rlm@10 1244 final Type fieldType){
rlm@10 1245 mv.visitFieldInsn(opcode,
rlm@10 1246 ownerType.getInternalName(),
rlm@10 1247 name,
rlm@10 1248 fieldType.getDescriptor());
rlm@10 1249 }
rlm@10 1250
rlm@10 1251 /**
rlm@10 1252 * Generates the instruction to push the value of a static field on the
rlm@10 1253 * stack.
rlm@10 1254 *
rlm@10 1255 * @param owner the class in which the field is defined.
rlm@10 1256 * @param name the name of the field.
rlm@10 1257 * @param type the type of the field.
rlm@10 1258 */
rlm@10 1259 public void getStatic(final Type owner, final String name, final Type type){
rlm@10 1260 fieldInsn(Opcodes.GETSTATIC, owner, name, type);
rlm@10 1261 }
rlm@10 1262
rlm@10 1263 /**
rlm@10 1264 * Generates the instruction to store the top stack value in a static field.
rlm@10 1265 *
rlm@10 1266 * @param owner the class in which the field is defined.
rlm@10 1267 * @param name the name of the field.
rlm@10 1268 * @param type the type of the field.
rlm@10 1269 */
rlm@10 1270 public void putStatic(final Type owner, final String name, final Type type){
rlm@10 1271 fieldInsn(Opcodes.PUTSTATIC, owner, name, type);
rlm@10 1272 }
rlm@10 1273
rlm@10 1274 /**
rlm@10 1275 * Generates the instruction to push the value of a non static field on the
rlm@10 1276 * stack.
rlm@10 1277 *
rlm@10 1278 * @param owner the class in which the field is defined.
rlm@10 1279 * @param name the name of the field.
rlm@10 1280 * @param type the type of the field.
rlm@10 1281 */
rlm@10 1282 public void getField(final Type owner, final String name, final Type type){
rlm@10 1283 fieldInsn(Opcodes.GETFIELD, owner, name, type);
rlm@10 1284 }
rlm@10 1285
rlm@10 1286 /**
rlm@10 1287 * Generates the instruction to store the top stack value in a non static
rlm@10 1288 * field.
rlm@10 1289 *
rlm@10 1290 * @param owner the class in which the field is defined.
rlm@10 1291 * @param name the name of the field.
rlm@10 1292 * @param type the type of the field.
rlm@10 1293 */
rlm@10 1294 public void putField(final Type owner, final String name, final Type type){
rlm@10 1295 fieldInsn(Opcodes.PUTFIELD, owner, name, type);
rlm@10 1296 }
rlm@10 1297
rlm@10 1298 // ------------------------------------------------------------------------
rlm@10 1299 // Instructions to invoke methods
rlm@10 1300 // ------------------------------------------------------------------------
rlm@10 1301
rlm@10 1302 /**
rlm@10 1303 * Generates an invoke method instruction.
rlm@10 1304 *
rlm@10 1305 * @param opcode the instruction's opcode.
rlm@10 1306 * @param type the class in which the method is defined.
rlm@10 1307 * @param method the method to be invoked.
rlm@10 1308 */
rlm@10 1309 private void invokeInsn(
rlm@10 1310 final int opcode,
rlm@10 1311 final Type type,
rlm@10 1312 final Method method){
rlm@10 1313 String owner = type.getSort() == Type.ARRAY
rlm@10 1314 ? type.getDescriptor()
rlm@10 1315 : type.getInternalName();
rlm@10 1316 mv.visitMethodInsn(opcode,
rlm@10 1317 owner,
rlm@10 1318 method.getName(),
rlm@10 1319 method.getDescriptor());
rlm@10 1320 }
rlm@10 1321
rlm@10 1322 /**
rlm@10 1323 * Generates the instruction to invoke a normal method.
rlm@10 1324 *
rlm@10 1325 * @param owner the class in which the method is defined.
rlm@10 1326 * @param method the method to be invoked.
rlm@10 1327 */
rlm@10 1328 public void invokeVirtual(final Type owner, final Method method){
rlm@10 1329 invokeInsn(Opcodes.INVOKEVIRTUAL, owner, method);
rlm@10 1330 }
rlm@10 1331
rlm@10 1332 /**
rlm@10 1333 * Generates the instruction to invoke a constructor.
rlm@10 1334 *
rlm@10 1335 * @param type the class in which the constructor is defined.
rlm@10 1336 * @param method the constructor to be invoked.
rlm@10 1337 */
rlm@10 1338 public void invokeConstructor(final Type type, final Method method){
rlm@10 1339 invokeInsn(Opcodes.INVOKESPECIAL, type, method);
rlm@10 1340 }
rlm@10 1341
rlm@10 1342 /**
rlm@10 1343 * Generates the instruction to invoke a static method.
rlm@10 1344 *
rlm@10 1345 * @param owner the class in which the method is defined.
rlm@10 1346 * @param method the method to be invoked.
rlm@10 1347 */
rlm@10 1348 public void invokeStatic(final Type owner, final Method method){
rlm@10 1349 invokeInsn(Opcodes.INVOKESTATIC, owner, method);
rlm@10 1350 }
rlm@10 1351
rlm@10 1352 /**
rlm@10 1353 * Generates the instruction to invoke an interface method.
rlm@10 1354 *
rlm@10 1355 * @param owner the class in which the method is defined.
rlm@10 1356 * @param method the method to be invoked.
rlm@10 1357 */
rlm@10 1358 public void invokeInterface(final Type owner, final Method method){
rlm@10 1359 invokeInsn(Opcodes.INVOKEINTERFACE, owner, method);
rlm@10 1360 }
rlm@10 1361
rlm@10 1362 // ------------------------------------------------------------------------
rlm@10 1363 // Instructions to create objects and arrays
rlm@10 1364 // ------------------------------------------------------------------------
rlm@10 1365
rlm@10 1366 /**
rlm@10 1367 * Generates a type dependent instruction.
rlm@10 1368 *
rlm@10 1369 * @param opcode the instruction's opcode.
rlm@10 1370 * @param type the instruction's operand.
rlm@10 1371 */
rlm@10 1372 private void typeInsn(final int opcode, final Type type){
rlm@10 1373 String desc;
rlm@10 1374 if(type.getSort() == Type.ARRAY)
rlm@10 1375 {
rlm@10 1376 desc = type.getDescriptor();
rlm@10 1377 }
rlm@10 1378 else
rlm@10 1379 {
rlm@10 1380 desc = type.getInternalName();
rlm@10 1381 }
rlm@10 1382 mv.visitTypeInsn(opcode, desc);
rlm@10 1383 }
rlm@10 1384
rlm@10 1385 /**
rlm@10 1386 * Generates the instruction to create a new object.
rlm@10 1387 *
rlm@10 1388 * @param type the class of the object to be created.
rlm@10 1389 */
rlm@10 1390 public void newInstance(final Type type){
rlm@10 1391 typeInsn(Opcodes.NEW, type);
rlm@10 1392 }
rlm@10 1393
rlm@10 1394 /**
rlm@10 1395 * Generates the instruction to create a new array.
rlm@10 1396 *
rlm@10 1397 * @param type the type of the array elements.
rlm@10 1398 */
rlm@10 1399 public void newArray(final Type type){
rlm@10 1400 int typ;
rlm@10 1401 switch(type.getSort())
rlm@10 1402 {
rlm@10 1403 case Type.BOOLEAN:
rlm@10 1404 typ = Opcodes.T_BOOLEAN;
rlm@10 1405 break;
rlm@10 1406 case Type.CHAR:
rlm@10 1407 typ = Opcodes.T_CHAR;
rlm@10 1408 break;
rlm@10 1409 case Type.BYTE:
rlm@10 1410 typ = Opcodes.T_BYTE;
rlm@10 1411 break;
rlm@10 1412 case Type.SHORT:
rlm@10 1413 typ = Opcodes.T_SHORT;
rlm@10 1414 break;
rlm@10 1415 case Type.INT:
rlm@10 1416 typ = Opcodes.T_INT;
rlm@10 1417 break;
rlm@10 1418 case Type.FLOAT:
rlm@10 1419 typ = Opcodes.T_FLOAT;
rlm@10 1420 break;
rlm@10 1421 case Type.LONG:
rlm@10 1422 typ = Opcodes.T_LONG;
rlm@10 1423 break;
rlm@10 1424 case Type.DOUBLE:
rlm@10 1425 typ = Opcodes.T_DOUBLE;
rlm@10 1426 break;
rlm@10 1427 default:
rlm@10 1428 typeInsn(Opcodes.ANEWARRAY, type);
rlm@10 1429 return;
rlm@10 1430 }
rlm@10 1431 mv.visitIntInsn(Opcodes.NEWARRAY, typ);
rlm@10 1432 }
rlm@10 1433
rlm@10 1434 // ------------------------------------------------------------------------
rlm@10 1435 // Miscelaneous instructions
rlm@10 1436 // ------------------------------------------------------------------------
rlm@10 1437
rlm@10 1438 /**
rlm@10 1439 * Generates the instruction to compute the length of an array.
rlm@10 1440 */
rlm@10 1441 public void arrayLength(){
rlm@10 1442 mv.visitInsn(Opcodes.ARRAYLENGTH);
rlm@10 1443 }
rlm@10 1444
rlm@10 1445 /**
rlm@10 1446 * Generates the instruction to throw an exception.
rlm@10 1447 */
rlm@10 1448 public void throwException(){
rlm@10 1449 mv.visitInsn(Opcodes.ATHROW);
rlm@10 1450 }
rlm@10 1451
rlm@10 1452 /**
rlm@10 1453 * Generates the instructions to create and throw an exception. The
rlm@10 1454 * exception class must have a constructor with a single String argument.
rlm@10 1455 *
rlm@10 1456 * @param type the class of the exception to be thrown.
rlm@10 1457 * @param msg the detailed message of the exception.
rlm@10 1458 */
rlm@10 1459 public void throwException(final Type type, final String msg){
rlm@10 1460 newInstance(type);
rlm@10 1461 dup();
rlm@10 1462 push(msg);
rlm@10 1463 invokeConstructor(type, Method.getMethod("void <init> (String)"));
rlm@10 1464 throwException();
rlm@10 1465 }
rlm@10 1466
rlm@10 1467 /**
rlm@10 1468 * Generates the instruction to check that the top stack value is of the
rlm@10 1469 * given type.
rlm@10 1470 *
rlm@10 1471 * @param type a class or interface type.
rlm@10 1472 */
rlm@10 1473 public void checkCast(final Type type){
rlm@10 1474 if(!type.equals(OBJECT_TYPE))
rlm@10 1475 {
rlm@10 1476 typeInsn(Opcodes.CHECKCAST, type);
rlm@10 1477 }
rlm@10 1478 }
rlm@10 1479
rlm@10 1480 /**
rlm@10 1481 * Generates the instruction to test if the top stack value is of the given
rlm@10 1482 * type.
rlm@10 1483 *
rlm@10 1484 * @param type a class or interface type.
rlm@10 1485 */
rlm@10 1486 public void instanceOf(final Type type){
rlm@10 1487 typeInsn(Opcodes.INSTANCEOF, type);
rlm@10 1488 }
rlm@10 1489
rlm@10 1490 /**
rlm@10 1491 * Generates the instruction to get the monitor of the top stack value.
rlm@10 1492 */
rlm@10 1493 public void monitorEnter(){
rlm@10 1494 mv.visitInsn(Opcodes.MONITORENTER);
rlm@10 1495 }
rlm@10 1496
rlm@10 1497 /**
rlm@10 1498 * Generates the instruction to release the monitor of the top stack value.
rlm@10 1499 */
rlm@10 1500 public void monitorExit(){
rlm@10 1501 mv.visitInsn(Opcodes.MONITOREXIT);
rlm@10 1502 }
rlm@10 1503
rlm@10 1504 // ------------------------------------------------------------------------
rlm@10 1505 // Non instructions
rlm@10 1506 // ------------------------------------------------------------------------
rlm@10 1507
rlm@10 1508 /**
rlm@10 1509 * Marks the end of the visited method.
rlm@10 1510 */
rlm@10 1511 public void endMethod(){
rlm@10 1512 if((access & Opcodes.ACC_ABSTRACT) == 0)
rlm@10 1513 {
rlm@10 1514 mv.visitMaxs(0, 0);
rlm@10 1515 }
rlm@10 1516 mv.visitEnd();
rlm@10 1517 }
rlm@10 1518
rlm@10 1519 /**
rlm@10 1520 * Marks the start of an exception handler.
rlm@10 1521 *
rlm@10 1522 * @param start beginning of the exception handler's scope (inclusive).
rlm@10 1523 * @param end end of the exception handler's scope (exclusive).
rlm@10 1524 * @param exception internal name of the type of exceptions handled by the
rlm@10 1525 * handler.
rlm@10 1526 */
rlm@10 1527 public void catchException(
rlm@10 1528 final Label start,
rlm@10 1529 final Label end,
rlm@10 1530 final Type exception){
rlm@10 1531 mv.visitTryCatchBlock(start, end, mark(), exception.getInternalName());
rlm@10 1532 }
rlm@10 1533 }