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