1/* 2 * Copyright (c) 2001, 2003, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25package sun.jvm.hotspot.interpreter; 26 27import sun.jvm.hotspot.oops.*; 28import sun.jvm.hotspot.utilities.*; 29 30public class BytecodeLookupswitch extends Bytecode { 31 BytecodeLookupswitch(Method method, int bci) { 32 super(method, bci); 33 } 34 35 // Attributes 36 public int defaultOffset() { return javaSignedWordAt(alignedOffset(1 + 0*jintSize)); } 37 public int numberOfPairs() { return javaSignedWordAt(alignedOffset(1 + 1*jintSize)); } 38 public LookupswitchPair pairAt(int i) { 39 if (Assert.ASSERTS_ENABLED) { 40 Assert.that(0 <= i && i < numberOfPairs(), "pair index out of bounds"); 41 } 42 return new LookupswitchPair(method, bci + alignedOffset(1 + (1 + i)*2*jintSize)); 43 } 44 45 public void verify() { 46 if (Assert.ASSERTS_ENABLED) { 47 Assert.that(isValid(), "check lookupswitch"); 48 } 49 } 50 51 public boolean isValid() { 52 boolean result = javaCode() == Bytecodes._lookupswitch; 53 if (result == false) return false; 54 int i = numberOfPairs() - 1; 55 while (i-- > 0) { 56 if(pairAt(i).match() > pairAt(i+1).match()) 57 return false; // unsorted lookup table 58 } 59 return true; 60 } 61 62 public static BytecodeLookupswitch at(Method method, int bci) { 63 BytecodeLookupswitch b = new BytecodeLookupswitch(method, bci); 64 if (Assert.ASSERTS_ENABLED) { 65 b.verify(); 66 } 67 return b; 68 } 69 70 /** Like at, but returns null if the BCI is not at lookupswitch */ 71 public static BytecodeLookupswitch atCheck(Method method, int bci) { 72 BytecodeLookupswitch b = new BytecodeLookupswitch(method, bci); 73 return (b.isValid() ? b : null); 74 } 75 76 public static BytecodeLookupswitch at(BytecodeStream bcs) { 77 return new BytecodeLookupswitch(bcs.method(), bcs.bci()); 78 } 79 80 public String toString() { 81 StringBuffer buf = new StringBuffer(); 82 buf.append("lookupswitch"); 83 buf.append(spaces); 84 buf.append("default: "); 85 buf.append(Integer.toString(bci() + defaultOffset())); 86 buf.append(comma); 87 int i = numberOfPairs() - 1; 88 while (i-- > 0) { 89 LookupswitchPair pair = pairAt(i); 90 buf.append("case "); 91 buf.append(Integer.toString(pair.match())); 92 buf.append(':'); 93 buf.append(Integer.toString(bci() + pair.offset())); 94 buf.append(comma); 95 } 96 97 return buf.toString(); 98 } 99} 100