Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/interpreter/BytecodeLookupswitch.java
41161 views
/*1* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.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 it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot.interpreter;2526import sun.jvm.hotspot.oops.*;27import sun.jvm.hotspot.utilities.*;2829public class BytecodeLookupswitch extends Bytecode {30BytecodeLookupswitch(Method method, int bci) {31super(method, bci);32}3334// Attributes35public int defaultOffset() { return javaSignedWordAt(alignedOffset(1 + 0*jintSize)); }36public int numberOfPairs() { return javaSignedWordAt(alignedOffset(1 + 1*jintSize)); }37public LookupswitchPair pairAt(int i) {38if (Assert.ASSERTS_ENABLED) {39Assert.that(0 <= i && i < numberOfPairs(), "pair index out of bounds");40}41return new LookupswitchPair(method, bci + alignedOffset(1 + (1 + i)*2*jintSize));42}4344public void verify() {45if (Assert.ASSERTS_ENABLED) {46Assert.that(isValid(), "check lookupswitch");47}48}4950public boolean isValid() {51boolean result = javaCode() == Bytecodes._lookupswitch;52if (result == false) return false;53int i = numberOfPairs() - 1;54while (i-- > 0) {55if(pairAt(i).match() > pairAt(i+1).match())56return false; // unsorted lookup table57}58return true;59}6061public static BytecodeLookupswitch at(Method method, int bci) {62BytecodeLookupswitch b = new BytecodeLookupswitch(method, bci);63if (Assert.ASSERTS_ENABLED) {64b.verify();65}66return b;67}6869/** Like at, but returns null if the BCI is not at lookupswitch */70public static BytecodeLookupswitch atCheck(Method method, int bci) {71BytecodeLookupswitch b = new BytecodeLookupswitch(method, bci);72return (b.isValid() ? b : null);73}7475public static BytecodeLookupswitch at(BytecodeStream bcs) {76return new BytecodeLookupswitch(bcs.method(), bcs.bci());77}7879public String toString() {80StringBuilder buf = new StringBuilder();81buf.append("lookupswitch");82buf.append(spaces);83buf.append("default: ");84buf.append(bci() + defaultOffset());85buf.append(comma);86int i = numberOfPairs() - 1;87while (i-- > 0) {88LookupswitchPair pair = pairAt(i);89buf.append("case ");90buf.append(pair.match());91buf.append(':');92buf.append(bci() + pair.offset());93buf.append(comma);94}9596return buf.toString();97}98}99100101