BytecodeSwitch.java revision 12651:6ef01bd40ce2
1/*
2 * Copyright (c) 2009, 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.
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 */
23package org.graalvm.compiler.bytecode;
24
25/**
26 * An abstract class that provides the state and methods common to {@link Bytecodes#LOOKUPSWITCH}
27 * and {@link Bytecodes#TABLESWITCH} instructions.
28 */
29public abstract class BytecodeSwitch {
30
31    /**
32     * The {@link BytecodeStream} containing the bytecode array.
33     */
34    protected final BytecodeStream stream;
35    /**
36     * Index of start of switch instruction.
37     */
38    protected final int bci;
39    /**
40     * Index of the start of the additional data for the switch instruction, aligned to a multiple
41     * of four from the method start.
42     */
43    protected final int alignedBci;
44
45    /**
46     * Constructor for a {@link BytecodeStream}.
47     *
48     * @param stream the {@code BytecodeStream} containing the switch instruction
49     * @param bci the index in the stream of the switch instruction
50     */
51    public BytecodeSwitch(BytecodeStream stream, int bci) {
52        this.stream = stream;
53        this.bci = bci;
54        this.alignedBci = (bci + 4) & 0xfffffffc;
55    }
56
57    /**
58     * Gets the current bytecode index.
59     *
60     * @return the current bytecode index
61     */
62    public int bci() {
63        return bci;
64    }
65
66    /**
67     * Gets the index of the instruction denoted by the {@code i}'th switch target.
68     *
69     * @param i index of the switch target
70     * @return the index of the instruction denoted by the {@code i}'th switch target
71     */
72    public int targetAt(int i) {
73        return bci + offsetAt(i);
74    }
75
76    /**
77     * Gets the index of the instruction for the default switch target.
78     *
79     * @return the index of the instruction for the default switch target
80     */
81    public int defaultTarget() {
82        return bci + defaultOffset();
83    }
84
85    /**
86     * Gets the offset from the start of the switch instruction to the default switch target.
87     *
88     * @return the offset to the default switch target
89     */
90    public int defaultOffset() {
91        return stream.readInt(alignedBci);
92    }
93
94    /**
95     * Gets the key at {@code i}'th switch target index.
96     *
97     * @param i the switch target index
98     * @return the key at {@code i}'th switch target index
99     */
100    public abstract int keyAt(int i);
101
102    /**
103     * Gets the offset from the start of the switch instruction for the {@code i}'th switch target.
104     *
105     * @param i the switch target index
106     * @return the offset to the {@code i}'th switch target
107     */
108    public abstract int offsetAt(int i);
109
110    /**
111     * Gets the number of switch targets.
112     *
113     * @return the number of switch targets
114     */
115    public abstract int numberOfCases();
116
117    /**
118     * Gets the total size in bytes of the switch instruction.
119     *
120     * @return the total size in bytes of the switch instruction
121     */
122    public abstract int size();
123
124}
125