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 
25 package sun.jvm.hotspot.interpreter;
26 
27 import sun.jvm.hotspot.oops.*;
28 import sun.jvm.hotspot.utilities.*;
29 
30 public class BytecodeTableswitch extends Bytecode {
BytecodeTableswitch(Method method, int bci)31   BytecodeTableswitch(Method method, int bci) {
32     super(method, bci);
33   }
34 
35 
36   // Attributes
defaultOffset()37   public int  defaultOffset()     { return javaSignedWordAt(alignedOffset(1 + 0*jintSize)); }
lowKey()38   public int  lowKey()            { return javaSignedWordAt(alignedOffset(1 + 1*jintSize)); }
highKey()39   public int  highKey()           { return javaSignedWordAt(alignedOffset(1 + 2*jintSize)); }
length()40   public int  length()            { return highKey()-lowKey()+1; }
destOffsetAt(int i)41   public int  destOffsetAt(int i) {
42     int x2 = alignedOffset(1 + (3 + i)*jintSize);
43     int val = javaSignedWordAt(x2);
44     return javaSignedWordAt(alignedOffset(1 + (3 + i)*jintSize));
45   }
46 
verify()47   public void verify() {
48     if (Assert.ASSERTS_ENABLED) {
49       Assert.that(isValid(), "check tableswitch");
50     }
51   }
52 
isValid()53   public boolean isValid() {
54     boolean result = javaCode() == Bytecodes._tableswitch;
55     if (result == false) return false;
56     int lo = lowKey();
57     int hi = highKey();
58     if (hi < lo) // incorrect hi/lo values in tableswitch
59        return false;
60 
61     int i  = hi - lo - 1 ;
62     while (i-- > 0) {
63       // no special check needed
64     }
65     return true;
66   }
67 
at(Method method, int bci)68   public static BytecodeTableswitch at(Method method, int bci) {
69     BytecodeTableswitch b = new BytecodeTableswitch(method, bci);
70     if (Assert.ASSERTS_ENABLED) {
71       b.verify();
72     }
73     return b;
74   }
75 
76   /** Like at, but returns null if the BCI is not at tableswitch  */
atCheck(Method method, int bci)77   public static BytecodeTableswitch atCheck(Method method, int bci) {
78     BytecodeTableswitch b = new BytecodeTableswitch(method, bci);
79     return (b.isValid() ? b : null);
80   }
81 
at(BytecodeStream bcs)82   public static BytecodeTableswitch at(BytecodeStream bcs) {
83     return new BytecodeTableswitch(bcs.method(), bcs.bci());
84   }
85 
toString()86   public String toString() {
87     StringBuffer buf = new StringBuffer();
88     buf.append("tableswitch");
89     buf.append(spaces);
90     buf.append("default: ");
91     buf.append(Integer.toString(bci() + defaultOffset()));
92     buf.append(comma);
93     int lo = lowKey();
94     int hi = highKey();
95     int i  = hi - lo - 1 ;
96     while (i-- > 0) {
97        buf.append("case ");
98        buf.append(Integer.toString(lo + i));
99        buf.append(':');
100        buf.append(Integer.toString(bci() + destOffsetAt(i)));
101        buf.append(comma);
102     }
103     return buf.toString();
104   }
105 }
106