0
|
1 /*
|
|
2 * Copyright 2002-2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 * have any 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 BytecodeLoadConstant extends BytecodeWithCPIndex {
|
|
31 BytecodeLoadConstant(Method method, int bci) {
|
|
32 super(method, bci);
|
|
33 }
|
|
34
|
|
35 public int index() {
|
|
36 return javaCode() == Bytecodes._ldc ?
|
|
37 (int) (0xFF & javaByteAt(1))
|
|
38 : (int) (0xFFFF & javaShortAt(1));
|
|
39 }
|
|
40
|
|
41 public void verify() {
|
|
42 if (Assert.ASSERTS_ENABLED) {
|
|
43 Assert.that(isValid(), "check load constant");
|
|
44 }
|
|
45 }
|
|
46
|
|
47 public boolean isValid() {
|
|
48 int jcode = javaCode();
|
|
49 boolean codeOk = jcode == Bytecodes._ldc || jcode == Bytecodes._ldc_w ||
|
|
50 jcode == Bytecodes._ldc2_w;
|
|
51 if (! codeOk) return false;
|
|
52
|
|
53 ConstantTag ctag = method().getConstants().getTagAt(index());
|
|
54 if (jcode == Bytecodes._ldc2_w) {
|
|
55 // has to be double or long
|
|
56 return (ctag.isDouble() || ctag.isLong()) ? true: false;
|
|
57 } else {
|
|
58 // has to be int or float or String or Klass
|
|
59 return (ctag.isUnresolvedString() || ctag.isString()
|
|
60 || ctag.isUnresolvedKlass() || ctag.isKlass()
|
|
61 || ctag.isInt() || ctag.isFloat())? true: false;
|
|
62 }
|
|
63 }
|
|
64
|
|
65 public boolean isKlassConstant() {
|
|
66 int jcode = javaCode();
|
|
67 if (jcode == Bytecodes._ldc2_w) {
|
|
68 return false;
|
|
69 }
|
|
70
|
|
71 ConstantTag ctag = method().getConstants().getTagAt(index());
|
|
72 return ctag.isKlass() || ctag.isUnresolvedKlass();
|
|
73 }
|
|
74
|
|
75 // return Symbol (if unresolved) or Klass (if resolved)
|
|
76 public Oop getKlass() {
|
|
77 if (Assert.ASSERTS_ENABLED) {
|
|
78 Assert.that(isKlassConstant(), "not a klass literal");
|
|
79 }
|
|
80 // tag change from 'unresolved' to 'klass' does not happen atomically.
|
|
81 // We just look at the object at the corresponding index and
|
|
82 // decide based on the oop type.
|
|
83 ConstantPool cpool = method().getConstants();
|
|
84 int cpIndex = index();
|
|
85 Oop oop = cpool.getObjAt(cpIndex);
|
|
86 if (oop.isKlass()) {
|
|
87 return (Klass) oop;
|
|
88 } else if (oop.isSymbol()) {
|
|
89 return (Symbol) oop;
|
|
90 } else {
|
|
91 throw new RuntimeException("should not reach here");
|
|
92 }
|
|
93 }
|
|
94
|
|
95 public static BytecodeLoadConstant at(Method method, int bci) {
|
|
96 BytecodeLoadConstant b = new BytecodeLoadConstant(method, bci);
|
|
97 if (Assert.ASSERTS_ENABLED) {
|
|
98 b.verify();
|
|
99 }
|
|
100 return b;
|
|
101 }
|
|
102
|
|
103 /** Like at, but returns null if the BCI is not at ldc or ldc_w or ldc2_w */
|
|
104 public static BytecodeLoadConstant atCheck(Method method, int bci) {
|
|
105 BytecodeLoadConstant b = new BytecodeLoadConstant(method, bci);
|
|
106 return (b.isValid() ? b : null);
|
|
107 }
|
|
108
|
|
109 public static BytecodeLoadConstant at(BytecodeStream bcs) {
|
|
110 return new BytecodeLoadConstant(bcs.method(), bcs.bci());
|
|
111 }
|
|
112
|
|
113 public String getConstantValue() {
|
|
114 ConstantPool cpool = method().getConstants();
|
|
115 int cpIndex = index();
|
|
116 ConstantTag ctag = cpool.getTagAt(cpIndex);
|
|
117 if (ctag.isInt()) {
|
|
118 return "<int " + Integer.toString(cpool.getIntAt(cpIndex)) +">";
|
|
119 } else if (ctag.isLong()) {
|
|
120 return "<long " + Long.toString(cpool.getLongAt(cpIndex)) + "L>";
|
|
121 } else if (ctag.isFloat()) {
|
|
122 return "<float " + Float.toString(cpool.getFloatAt(cpIndex)) + "F>";
|
|
123 } else if (ctag.isDouble()) {
|
|
124 return "<double " + Double.toString(cpool.getDoubleAt(cpIndex)) + "D>";
|
|
125 } else if (ctag.isString() || ctag.isUnresolvedString()) {
|
|
126 // tag change from 'unresolved' to 'string' does not happen atomically.
|
|
127 // We just look at the object at the corresponding index and
|
|
128 // decide based on the oop type.
|
|
129 Oop obj = cpool.getObjAt(cpIndex);
|
|
130 if (obj.isSymbol()) {
|
|
131 Symbol sym = (Symbol) obj;
|
|
132 return "<String \"" + sym.asString() + "\">";
|
|
133 } else if (obj.isInstance()) {
|
|
134 return "<String \"" + OopUtilities.stringOopToString(obj) + "\">";
|
|
135 } else {
|
|
136 throw new RuntimeException("should not reach here");
|
|
137 }
|
|
138 } else if (ctag.isKlass() || ctag.isUnresolvedKlass()) {
|
|
139 // tag change from 'unresolved' to 'klass' does not happen atomically.
|
|
140 // We just look at the object at the corresponding index and
|
|
141 // decide based on the oop type.
|
|
142 Oop obj = cpool.getObjAt(cpIndex);
|
|
143 if (obj.isKlass()) {
|
|
144 Klass k = (Klass) obj;
|
|
145 return "<Class " + k.getName().asString() + "@" + k.getHandle() + ">";
|
|
146 } else if (obj.isSymbol()) {
|
|
147 Symbol sym = (Symbol) obj;
|
|
148 return "<Class " + sym.asString() + ">";
|
|
149 } else {
|
|
150 throw new RuntimeException("should not reach here");
|
|
151 }
|
|
152 } else {
|
|
153 if (Assert.ASSERTS_ENABLED) {
|
|
154 Assert.that(false, "invalid load constant type");
|
|
155 }
|
|
156 return null;
|
|
157 }
|
|
158 }
|
|
159
|
|
160 public String toString() {
|
|
161 StringBuffer buf = new StringBuffer();
|
|
162 buf.append(getJavaBytecodeName());
|
|
163 buf.append(spaces);
|
|
164 buf.append('#');
|
|
165 buf.append(Integer.toString(index()));
|
|
166 buf.append(spaces);
|
|
167 buf.append(getConstantValue());
|
|
168 if (code() != javaCode()) {
|
|
169 buf.append(spaces);
|
|
170 buf.append('[');
|
|
171 buf.append(getBytecodeName());
|
|
172 buf.append(']');
|
|
173 }
|
|
174 return buf.toString();
|
|
175 }
|
|
176 }
|