1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. Oracle designates this 7 * particular file as subject to the "Classpath" exception as provided 8 * by Oracle in the LICENSE file that accompanied this code. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 /* 26 * This file is available under and governed by the GNU General Public 27 * License version 2 only, as published by the Free Software Foundation. 28 * However, the following notice accompanied the original version of this 29 * file: 30 * 31 * ASM: a very small and fast Java bytecode manipulation framework 32 * Copyright (c) 2000-2011 INRIA, France Telecom 33 * All rights reserved. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. Neither the name of the copyright holders nor the names of its 44 * contributors may be used to endorse or promote products derived from 45 * this software without specific prior written permission. 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 48 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 50 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 51 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 52 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 53 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 54 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 55 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 56 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 57 * THE POSSIBILITY OF SUCH DAMAGE. 58 */ 59 package jdk.internal.org.objectweb.asm; 60 61 /** 62 * A label represents a position in the bytecode of a method. Labels are used 63 * for jump, goto, and switch instructions, and for try catch blocks. A label 64 * designates the <i>instruction</i> that is just after. Note however that there 65 * can be other elements between a label and the instruction it designates (such 66 * as other labels, stack map frames, line numbers, etc.). 67 * 68 * @author Eric Bruneton 69 */ 70 public class Label { 71 72 /** 73 * Indicates if this label is only used for debug attributes. Such a label 74 * is not the start of a basic block, the target of a jump instruction, or 75 * an exception handler. It can be safely ignored in control flow graph 76 * analysis algorithms (for optimization purposes). 77 */ 78 static final int DEBUG = 1; 79 80 /** 81 * Indicates if the position of this label is known. 82 */ 83 static final int RESOLVED = 2; 84 85 /** 86 * Indicates if this label has been updated, after instruction resizing. 87 */ 88 static final int RESIZED = 4; 89 90 /** 91 * Indicates if this basic block has been pushed in the basic block stack. 92 * See {@link MethodWriter#visitMaxs visitMaxs}. 93 */ 94 static final int PUSHED = 8; 95 96 /** 97 * Indicates if this label is the target of a jump instruction, or the start 98 * of an exception handler. 99 */ 100 static final int TARGET = 16; 101 102 /** 103 * Indicates if a stack map frame must be stored for this label. 104 */ 105 static final int STORE = 32; 106 107 /** 108 * Indicates if this label corresponds to a reachable basic block. 109 */ 110 static final int REACHABLE = 64; 111 112 /** 113 * Indicates if this basic block ends with a JSR instruction. 114 */ 115 static final int JSR = 128; 116 117 /** 118 * Indicates if this basic block ends with a RET instruction. 119 */ 120 static final int RET = 256; 121 122 /** 123 * Indicates if this basic block is the start of a subroutine. 124 */ 125 static final int SUBROUTINE = 512; 126 127 /** 128 * Indicates if this subroutine basic block has been visited by a 129 * visitSubroutine(null, ...) call. 130 */ 131 static final int VISITED = 1024; 132 133 /** 134 * Indicates if this subroutine basic block has been visited by a 135 * visitSubroutine(!null, ...) call. 136 */ 137 static final int VISITED2 = 2048; 138 139 /** 140 * Field used to associate user information to a label. Warning: this field 141 * is used by the ASM tree package. In order to use it with the ASM tree 142 * package you must override the 143 * {@link jdk.internal.org.objectweb.asm.tree.MethodNode#getLabelNode} method. 144 */ 145 public Object info; 146 147 /** 148 * Flags that indicate the status of this label. 149 * 150 * @see #DEBUG 151 * @see #RESOLVED 152 * @see #RESIZED 153 * @see #PUSHED 154 * @see #TARGET 155 * @see #STORE 156 * @see #REACHABLE 157 * @see #JSR 158 * @see #RET 159 */ 160 int status; 161 162 /** 163 * The line number corresponding to this label, if known. If there are 164 * several lines, each line is stored in a separate label, all linked via 165 * their next field (these links are created in ClassReader and removed just 166 * before visitLabel is called, so that this does not impact the rest of the 167 * code). 168 */ 169 int line; 170 171 /** 172 * The position of this label in the code, if known. 173 */ 174 int position; 175 176 /** 177 * Number of forward references to this label, times two. 178 */ 179 private int referenceCount; 180 181 /** 182 * Informations about forward references. Each forward reference is 183 * described by two consecutive integers in this array: the first one is the 184 * position of the first byte of the bytecode instruction that contains the 185 * forward reference, while the second is the position of the first byte of 186 * the forward reference itself. In fact the sign of the first integer 187 * indicates if this reference uses 2 or 4 bytes, and its absolute value 188 * gives the position of the bytecode instruction. This array is also used 189 * as a bitset to store the subroutines to which a basic block belongs. This 190 * information is needed in {@linked MethodWriter#visitMaxs}, after all 191 * forward references have been resolved. Hence the same array can be used 192 * for both purposes without problems. 193 */ 194 private int[] srcAndRefPositions; 195 196 // ------------------------------------------------------------------------ 197 198 /* 199 * Fields for the control flow and data flow graph analysis algorithms (used 200 * to compute the maximum stack size or the stack map frames). A control 201 * flow graph contains one node per "basic block", and one edge per "jump" 202 * from one basic block to another. Each node (i.e., each basic block) is 203 * represented by the Label object that corresponds to the first instruction 204 * of this basic block. Each node also stores the list of its successors in 205 * the graph, as a linked list of Edge objects. 206 * 207 * The control flow analysis algorithms used to compute the maximum stack 208 * size or the stack map frames are similar and use two steps. The first 209 * step, during the visit of each instruction, builds information about the 210 * state of the local variables and the operand stack at the end of each 211 * basic block, called the "output frame", <i>relatively</i> to the frame 212 * state at the beginning of the basic block, which is called the "input 213 * frame", and which is <i>unknown</i> during this step. The second step, in 214 * {@link MethodWriter#visitMaxs}, is a fix point algorithm that computes 215 * information about the input frame of each basic block, from the input 216 * state of the first basic block (known from the method signature), and by 217 * the using the previously computed relative output frames. 218 * 219 * The algorithm used to compute the maximum stack size only computes the 220 * relative output and absolute input stack heights, while the algorithm 221 * used to compute stack map frames computes relative output frames and 222 * absolute input frames. 223 */ 224 225 /** 226 * Start of the output stack relatively to the input stack. The exact 227 * semantics of this field depends on the algorithm that is used. 228 * 229 * When only the maximum stack size is computed, this field is the number of 230 * elements in the input stack. 231 * 232 * When the stack map frames are completely computed, this field is the 233 * offset of the first output stack element relatively to the top of the 234 * input stack. This offset is always negative or null. A null offset means 235 * that the output stack must be appended to the input stack. A -n offset 236 * means that the first n output stack elements must replace the top n input 237 * stack elements, and that the other elements must be appended to the input 238 * stack. 239 */ 240 int inputStackTop; 241 242 /** 243 * Maximum height reached by the output stack, relatively to the top of the 244 * input stack. This maximum is always positive or null. 245 */ 246 int outputStackMax; 247 248 /** 249 * Information about the input and output stack map frames of this basic 250 * block. This field is only used when {@link ClassWriter#COMPUTE_FRAMES} 251 * option is used. 252 */ 253 Frame frame; 254 255 /** 256 * The successor of this label, in the order they are visited. This linked 257 * list does not include labels used for debug info only. If 258 * {@link ClassWriter#COMPUTE_FRAMES} option is used then, in addition, it 259 * does not contain successive labels that denote the same bytecode position 260 * (in this case only the first label appears in this list). 261 */ 262 Label successor; 263 264 /** 265 * The successors of this node in the control flow graph. These successors 266 * are stored in a linked list of {@link Edge Edge} objects, linked to each 267 * other by their {@link Edge#next} field. 268 */ 269 Edge successors; 270 271 /** 272 * The next basic block in the basic block stack. This stack is used in the 273 * main loop of the fix point algorithm used in the second step of the 274 * control flow analysis algorithms. It is also used in 275 * {@link #visitSubroutine} to avoid using a recursive method, and in 276 * ClassReader to temporarily store multiple source lines for a label. 277 * 278 * @see MethodWriter#visitMaxs 279 */ 280 Label next; 281 282 // ------------------------------------------------------------------------ 283 // Constructor 284 // ------------------------------------------------------------------------ 285 286 /** 287 * Constructs a new label. 288 */ 289 public Label() { 290 } 291 292 // ------------------------------------------------------------------------ 293 // Methods to compute offsets and to manage forward references 294 // ------------------------------------------------------------------------ 295 296 /** 297 * Returns the offset corresponding to this label. This offset is computed 298 * from the start of the method's bytecode. <i>This method is intended for 299 * {@link Attribute} sub classes, and is normally not needed by class 300 * generators or adapters.</i> 301 * 302 * @return the offset corresponding to this label. 303 * @throws IllegalStateException 304 * if this label is not resolved yet. 305 */ 306 public int getOffset() { 307 if ((status & RESOLVED) == 0) { 308 throw new IllegalStateException( 309 "Label offset position has not been resolved yet"); 310 } 311 return position; 312 } 313 314 /** 315 * Puts a reference to this label in the bytecode of a method. If the 316 * position of the label is known, the offset is computed and written 317 * directly. Otherwise, a null offset is written and a new forward reference 318 * is declared for this label. 319 * 320 * @param owner 321 * the code writer that calls this method. 322 * @param out 323 * the bytecode of the method. 324 * @param source 325 * the position of first byte of the bytecode instruction that 326 * contains this label. 327 * @param wideOffset 328 * <tt>true</tt> if the reference must be stored in 4 bytes, or 329 * <tt>false</tt> if it must be stored with 2 bytes. 330 * @throws IllegalArgumentException 331 * if this label has not been created by the given code writer. 332 */ 333 void put(final MethodWriter owner, final ByteVector out, final int source, 334 final boolean wideOffset) { 335 if ((status & RESOLVED) == 0) { 336 if (wideOffset) { 337 addReference(-1 - source, out.length); 338 out.putInt(-1); 339 } else { 340 addReference(source, out.length); 341 out.putShort(-1); 342 } 343 } else { 344 if (wideOffset) { 345 out.putInt(position - source); 346 } else { 347 out.putShort(position - source); 348 } 349 } 350 } 351 352 /** 353 * Adds a forward reference to this label. This method must be called only 354 * for a true forward reference, i.e. only if this label is not resolved 355 * yet. For backward references, the offset of the reference can be, and 356 * must be, computed and stored directly. 357 * 358 * @param sourcePosition 359 * the position of the referencing instruction. This position 360 * will be used to compute the offset of this forward reference. 361 * @param referencePosition 362 * the position where the offset for this forward reference must 363 * be stored. 364 */ 365 private void addReference(final int sourcePosition, 366 final int referencePosition) { 367 if (srcAndRefPositions == null) { 368 srcAndRefPositions = new int[6]; 369 } 370 if (referenceCount >= srcAndRefPositions.length) { 371 int[] a = new int[srcAndRefPositions.length + 6]; 372 System.arraycopy(srcAndRefPositions, 0, a, 0, 373 srcAndRefPositions.length); 374 srcAndRefPositions = a; 375 } 376 srcAndRefPositions[referenceCount++] = sourcePosition; 377 srcAndRefPositions[referenceCount++] = referencePosition; 378 } 379 380 /** 381 * Resolves all forward references to this label. This method must be called 382 * when this label is added to the bytecode of the method, i.e. when its 383 * position becomes known. This method fills in the blanks that where left 384 * in the bytecode by each forward reference previously added to this label. 385 * 386 * @param owner 387 * the code writer that calls this method. 388 * @param position 389 * the position of this label in the bytecode. 390 * @param data 391 * the bytecode of the method. 392 * @return <tt>true</tt> if a blank that was left for this label was to 393 * small to store the offset. In such a case the corresponding jump 394 * instruction is replaced with a pseudo instruction (using unused 395 * opcodes) using an unsigned two bytes offset. These pseudo 396 * instructions will need to be replaced with true instructions with 397 * wider offsets (4 bytes instead of 2). This is done in 398 * {@link MethodWriter#resizeInstructions}. 399 * @throws IllegalArgumentException 400 * if this label has already been resolved, or if it has not 401 * been created by the given code writer. 402 */ 403 boolean resolve(final MethodWriter owner, final int position, 404 final byte[] data) { 405 boolean needUpdate = false; 406 this.status |= RESOLVED; 407 this.position = position; 408 int i = 0; 409 while (i < referenceCount) { 410 int source = srcAndRefPositions[i++]; 411 int reference = srcAndRefPositions[i++]; 412 int offset; 413 if (source >= 0) { 414 offset = position - source; 415 if (offset < Short.MIN_VALUE || offset > Short.MAX_VALUE) { 416 /* 417 * changes the opcode of the jump instruction, in order to 418 * be able to find it later (see resizeInstructions in 419 * MethodWriter). These temporary opcodes are similar to 420 * jump instruction opcodes, except that the 2 bytes offset 421 * is unsigned (and can therefore represent values from 0 to 422 * 65535, which is sufficient since the size of a method is 423 * limited to 65535 bytes). 424 */ 425 int opcode = data[reference - 1] & 0xFF; 426 if (opcode <= Opcodes.JSR) { 427 // changes IFEQ ... JSR to opcodes 202 to 217 428 data[reference - 1] = (byte) (opcode + 49); 429 } else { 430 // changes IFNULL and IFNONNULL to opcodes 218 and 219 431 data[reference - 1] = (byte) (opcode + 20); 432 } 433 needUpdate = true; 434 } 435 data[reference++] = (byte) (offset >>> 8); 436 data[reference] = (byte) offset; 437 } else { 438 offset = position + source + 1; 439 data[reference++] = (byte) (offset >>> 24); 440 data[reference++] = (byte) (offset >>> 16); 441 data[reference++] = (byte) (offset >>> 8); 442 data[reference] = (byte) offset; 443 } 444 } 445 return needUpdate; 446 } 447 448 /** 449 * Returns the first label of the series to which this label belongs. For an 450 * isolated label or for the first label in a series of successive labels, 451 * this method returns the label itself. For other labels it returns the 452 * first label of the series. 453 * 454 * @return the first label of the series to which this label belongs. 455 */ 456 Label getFirst() { 457 return !ClassReader.FRAMES || frame == null ? this : frame.owner; 458 } 459 460 // ------------------------------------------------------------------------ 461 // Methods related to subroutines 462 // ------------------------------------------------------------------------ 463 464 /** 465 * Returns true is this basic block belongs to the given subroutine. 466 * 467 * @param id 468 * a subroutine id. 469 * @return true is this basic block belongs to the given subroutine. 470 */ 471 boolean inSubroutine(final long id) { 472 if ((status & Label.VISITED) != 0) { 473 return (srcAndRefPositions[(int) (id >>> 32)] & (int) id) != 0; 474 } 475 return false; 476 } 477 478 /** 479 * Returns true if this basic block and the given one belong to a common 480 * subroutine. 481 * 482 * @param block 483 * another basic block. 484 * @return true if this basic block and the given one belong to a common 485 * subroutine. 486 */ 487 boolean inSameSubroutine(final Label block) { 488 if ((status & VISITED) == 0 || (block.status & VISITED) == 0) { 489 return false; 490 } 491 for (int i = 0; i < srcAndRefPositions.length; ++i) { 492 if ((srcAndRefPositions[i] & block.srcAndRefPositions[i]) != 0) { 493 return true; 494 } 495 } 496 return false; 497 } 498 499 /** 500 * Marks this basic block as belonging to the given subroutine. 501 * 502 * @param id 503 * a subroutine id. 504 * @param nbSubroutines 505 * the total number of subroutines in the method. 506 */ 507 void addToSubroutine(final long id, final int nbSubroutines) { 508 if ((status & VISITED) == 0) { 509 status |= VISITED; 510 srcAndRefPositions = new int[nbSubroutines / 32 + 1]; 511 } 512 srcAndRefPositions[(int) (id >>> 32)] |= (int) id; 513 } 514 515 /** 516 * Finds the basic blocks that belong to a given subroutine, and marks these 517 * blocks as belonging to this subroutine. This method follows the control 518 * flow graph to find all the blocks that are reachable from the current 519 * block WITHOUT following any JSR target. 520 * 521 * @param JSR 522 * a JSR block that jumps to this subroutine. If this JSR is not 523 * null it is added to the successor of the RET blocks found in 524 * the subroutine. 525 * @param id 526 * the id of this subroutine. 527 * @param nbSubroutines 528 * the total number of subroutines in the method. 529 */ 530 void visitSubroutine(final Label JSR, final long id, final int nbSubroutines) { 531 // user managed stack of labels, to avoid using a recursive method 532 // (recursivity can lead to stack overflow with very large methods) 533 Label stack = this; 534 while (stack != null) { 535 // removes a label l from the stack 536 Label l = stack; 537 stack = l.next; 538 l.next = null; 539 540 if (JSR != null) { 541 if ((l.status & VISITED2) != 0) { 542 continue; 543 } 544 l.status |= VISITED2; 545 // adds JSR to the successors of l, if it is a RET block 546 if ((l.status & RET) != 0) { 547 if (!l.inSameSubroutine(JSR)) { 548 Edge e = new Edge(); 549 e.info = l.inputStackTop; 550 e.successor = JSR.successors.successor; 551 e.next = l.successors; 552 l.successors = e; 553 } 554 } 555 } else { 556 // if the l block already belongs to subroutine 'id', continue 557 if (l.inSubroutine(id)) { 558 continue; 559 } 560 // marks the l block as belonging to subroutine 'id' 561 l.addToSubroutine(id, nbSubroutines); 562 } 563 // pushes each successor of l on the stack, except JSR targets 564 Edge e = l.successors; 565 while (e != null) { 566 // if the l block is a JSR block, then 'l.successors.next' leads 567 // to the JSR target (see {@link #visitJumpInsn}) and must 568 // therefore not be followed 569 if ((l.status & Label.JSR) == 0 || e != l.successors.next) { 570 // pushes e.successor on the stack if it not already added 571 if (e.successor.next == null) { 572 e.successor.next = stack; 573 stack = e.successor; 574 } 575 } 576 e = e.next; 577 } 578 } 579 } 580 581 // ------------------------------------------------------------------------ 582 // Overriden Object methods 583 // ------------------------------------------------------------------------ 584 585 /** 586 * Returns a string representation of this label. 587 * 588 * @return a string representation of this label. 589 */ 590 @Override 591 public String toString() { 592 return "L" + System.identityHashCode(this); 593 } 594 }