jdk/src/java.base/share/classes/sun/reflect/Label.java
changeset 37377 bd3b98a11a14
parent 37376 5daa5fbad3ce
parent 37374 ee1b8619eddb
child 37380 55c2549714af
equal deleted inserted replaced
37376:5daa5fbad3ce 37377:bd3b98a11a14
     1 /*
       
     2  * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package sun.reflect;
       
    27 
       
    28 import java.util.List;
       
    29 import java.util.ArrayList;
       
    30 
       
    31 /** Allows forward references in bytecode streams emitted by
       
    32     ClassFileAssembler. Assumes that the start of the method body is
       
    33     the first byte in the assembler's buffer. May be used at more than
       
    34     one branch site. */
       
    35 
       
    36 class Label {
       
    37     static class PatchInfo {
       
    38         PatchInfo(ClassFileAssembler asm,
       
    39                   short instrBCI,
       
    40                   short patchBCI,
       
    41                   int stackDepth)
       
    42         {
       
    43             this.asm = asm;
       
    44             this.instrBCI   = instrBCI;
       
    45             this.patchBCI   = patchBCI;
       
    46             this.stackDepth = stackDepth;
       
    47         }
       
    48         // This won't work for more than one assembler anyway, so this is
       
    49         // unnecessary
       
    50         final ClassFileAssembler asm;
       
    51         final short instrBCI;
       
    52         final short patchBCI;
       
    53         final int   stackDepth;
       
    54     }
       
    55     private final List<PatchInfo> patches = new ArrayList<>();
       
    56 
       
    57     public Label() {
       
    58     }
       
    59 
       
    60     void add(ClassFileAssembler asm,
       
    61              short instrBCI,
       
    62              short patchBCI,
       
    63              int stackDepth)
       
    64     {
       
    65         patches.add(new PatchInfo(asm, instrBCI, patchBCI, stackDepth));
       
    66     }
       
    67 
       
    68     public void bind() {
       
    69         for (PatchInfo patch : patches){
       
    70             short curBCI = patch.asm.getLength();
       
    71             short offset = (short) (curBCI - patch.instrBCI);
       
    72             patch.asm.emitShort(patch.patchBCI, offset);
       
    73             patch.asm.setStack(patch.stackDepth);
       
    74         }
       
    75     }
       
    76 }