001/*
002 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.bytecode;
024
025/**
026 * An abstract class that provides the state and methods common to {@link Bytecodes#LOOKUPSWITCH}
027 * and {@link Bytecodes#TABLESWITCH} instructions.
028 */
029public abstract class BytecodeSwitch {
030
031    /**
032     * The {@link BytecodeStream} containing the bytecode array.
033     */
034    protected final BytecodeStream stream;
035    /**
036     * Index of start of switch instruction.
037     */
038    protected final int bci;
039    /**
040     * Index of the start of the additional data for the switch instruction, aligned to a multiple
041     * of four from the method start.
042     */
043    protected final int alignedBci;
044
045    /**
046     * Constructor for a {@link BytecodeStream}.
047     *
048     * @param stream the {@code BytecodeStream} containing the switch instruction
049     * @param bci the index in the stream of the switch instruction
050     */
051    public BytecodeSwitch(BytecodeStream stream, int bci) {
052        this.stream = stream;
053        this.bci = bci;
054        this.alignedBci = (bci + 4) & 0xfffffffc;
055    }
056
057    /**
058     * Gets the current bytecode index.
059     *
060     * @return the current bytecode index
061     */
062    public int bci() {
063        return bci;
064    }
065
066    /**
067     * Gets the index of the instruction denoted by the {@code i}'th switch target.
068     *
069     * @param i index of the switch target
070     * @return the index of the instruction denoted by the {@code i}'th switch target
071     */
072    public int targetAt(int i) {
073        return bci + offsetAt(i);
074    }
075
076    /**
077     * Gets the index of the instruction for the default switch target.
078     *
079     * @return the index of the instruction for the default switch target
080     */
081    public int defaultTarget() {
082        return bci + defaultOffset();
083    }
084
085    /**
086     * Gets the offset from the start of the switch instruction to the default switch target.
087     *
088     * @return the offset to the default switch target
089     */
090    public int defaultOffset() {
091        return stream.readInt(alignedBci);
092    }
093
094    /**
095     * Gets the key at {@code i}'th switch target index.
096     *
097     * @param i the switch target index
098     * @return the key at {@code i}'th switch target index
099     */
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}