comparison graal/com.oracle.max.cri/src/com/sun/cri/bytecode/BytecodeSwitch.java @ 3733:e233f5660da4

Added Java files from Maxine project.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Sat, 17 Dec 2011 19:59:18 +0100
parents
children bc8527f3071c
comparison
equal deleted inserted replaced
3732:3e2e8b8abdaf 3733:e233f5660da4
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 package com.sun.cri.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 */
29 public abstract class BytecodeSwitch {
30 /**
31 * The {@link BytecodeStream} containing bytecode array or {@code null} if {@link #code} is not {@code null}.
32 */
33 private final BytecodeStream stream;
34 /**
35 * The bytecode array or {@code null} if {@link #stream} is not {@code null}.
36 */
37 private final byte[] code;
38 /**
39 * Index of start of switch instruction.
40 */
41 protected final int bci;
42 /**
43 * Index of the start of the additional data for the switch instruction, aligned to a multiple of four from the method start.
44 */
45 protected final int alignedBci;
46
47 /**
48 * Constructor for a {@link BytecodeStream}.
49 * @param stream the {@code BytecodeStream} containing the switch instruction
50 * @param bci the index in the stream of the switch instruction
51 */
52 public BytecodeSwitch(BytecodeStream stream, int bci) {
53 this.alignedBci = (bci + 4) & 0xfffffffc;
54 this.stream = stream;
55 this.code = null;
56 this.bci = bci;
57 }
58
59 /**
60 * Constructor for a bytecode array.
61 * @param code the bytecode array containing the switch instruction.
62 * @param bci the index in the array of the switch instruction
63 */
64 public BytecodeSwitch(byte[] code, int bci) {
65 this.alignedBci = (bci + 4) & 0xfffffffc;
66 this.stream = null;
67 this.code = code;
68 this.bci = bci;
69 }
70
71 /**
72 * Gets the current bytecode index.
73 * @return the current bytecode index
74 */
75 public int bci() {
76 return bci;
77 }
78
79 /**
80 * Gets the index of the instruction denoted by the {@code i}'th switch target.
81 * @param i index of the switch target
82 * @return the index of the instruction denoted by the {@code i}'th switch target
83 */
84 public int targetAt(int i) {
85 return bci + offsetAt(i);
86 }
87
88 /**
89 * Gets the index of the instruction for the default switch target.
90 * @return the index of the instruction for the default switch target
91 */
92 public int defaultTarget() {
93 return bci + defaultOffset();
94 }
95
96 /**
97 * Gets the offset from the start of the switch instruction to the default switch target.
98 * @return the offset to the default switch target
99 */
100 public abstract int defaultOffset();
101
102 /**
103 * Gets the key at {@code i}'th switch target index.
104 * @param i the switch target index
105 * @return the key at {@code i}'th switch target index
106 */
107 public abstract int keyAt(int i);
108
109 /**
110 * Gets the offset from the start of the switch instruction for the {@code i}'th switch target.
111 * @param i the switch target index
112 * @return the offset to the {@code i}'th switch target
113 */
114 public abstract int offsetAt(int i);
115
116 /**
117 * Gets the number of switch targets.
118 * @return the number of switch targets
119 */
120 public abstract int numberOfCases();
121
122 /**
123 * Gets the total size in bytes of the switch instruction.
124 * @return the total size in bytes of the switch instruction
125 */
126 public abstract int size();
127
128 /**
129 * Reads the signed value at given bytecode index.
130 * @param bci the start index of the value to retrieve
131 * @return the signed, 4-byte value in the bytecode array starting at {@code bci}
132 */
133 protected int readWord(int bci) {
134 if (code != null) {
135 return Bytes.beS4(code, bci);
136 }
137 return stream.readInt(bci);
138 }
139 }