annotate agent/src/share/classes/sun/jvm/hotspot/interpreter/BytecodeLoadConstant.java @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children c18cbe5936b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 2002-2003 Sun Microsystems, Inc. All Rights Reserved.
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 package sun.jvm.hotspot.interpreter;
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 import sun.jvm.hotspot.oops.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
28 import sun.jvm.hotspot.utilities.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
29
a61af66fc99e Initial load
duke
parents:
diff changeset
30 public class BytecodeLoadConstant extends BytecodeWithCPIndex {
a61af66fc99e Initial load
duke
parents:
diff changeset
31 BytecodeLoadConstant(Method method, int bci) {
a61af66fc99e Initial load
duke
parents:
diff changeset
32 super(method, bci);
a61af66fc99e Initial load
duke
parents:
diff changeset
33 }
a61af66fc99e Initial load
duke
parents:
diff changeset
34
a61af66fc99e Initial load
duke
parents:
diff changeset
35 public int index() {
a61af66fc99e Initial load
duke
parents:
diff changeset
36 return javaCode() == Bytecodes._ldc ?
a61af66fc99e Initial load
duke
parents:
diff changeset
37 (int) (0xFF & javaByteAt(1))
a61af66fc99e Initial load
duke
parents:
diff changeset
38 : (int) (0xFFFF & javaShortAt(1));
a61af66fc99e Initial load
duke
parents:
diff changeset
39 }
a61af66fc99e Initial load
duke
parents:
diff changeset
40
a61af66fc99e Initial load
duke
parents:
diff changeset
41 public void verify() {
a61af66fc99e Initial load
duke
parents:
diff changeset
42 if (Assert.ASSERTS_ENABLED) {
a61af66fc99e Initial load
duke
parents:
diff changeset
43 Assert.that(isValid(), "check load constant");
a61af66fc99e Initial load
duke
parents:
diff changeset
44 }
a61af66fc99e Initial load
duke
parents:
diff changeset
45 }
a61af66fc99e Initial load
duke
parents:
diff changeset
46
a61af66fc99e Initial load
duke
parents:
diff changeset
47 public boolean isValid() {
a61af66fc99e Initial load
duke
parents:
diff changeset
48 int jcode = javaCode();
a61af66fc99e Initial load
duke
parents:
diff changeset
49 boolean codeOk = jcode == Bytecodes._ldc || jcode == Bytecodes._ldc_w ||
a61af66fc99e Initial load
duke
parents:
diff changeset
50 jcode == Bytecodes._ldc2_w;
a61af66fc99e Initial load
duke
parents:
diff changeset
51 if (! codeOk) return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
52
a61af66fc99e Initial load
duke
parents:
diff changeset
53 ConstantTag ctag = method().getConstants().getTagAt(index());
a61af66fc99e Initial load
duke
parents:
diff changeset
54 if (jcode == Bytecodes._ldc2_w) {
a61af66fc99e Initial load
duke
parents:
diff changeset
55 // has to be double or long
a61af66fc99e Initial load
duke
parents:
diff changeset
56 return (ctag.isDouble() || ctag.isLong()) ? true: false;
a61af66fc99e Initial load
duke
parents:
diff changeset
57 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
58 // has to be int or float or String or Klass
a61af66fc99e Initial load
duke
parents:
diff changeset
59 return (ctag.isUnresolvedString() || ctag.isString()
a61af66fc99e Initial load
duke
parents:
diff changeset
60 || ctag.isUnresolvedKlass() || ctag.isKlass()
a61af66fc99e Initial load
duke
parents:
diff changeset
61 || ctag.isInt() || ctag.isFloat())? true: false;
a61af66fc99e Initial load
duke
parents:
diff changeset
62 }
a61af66fc99e Initial load
duke
parents:
diff changeset
63 }
a61af66fc99e Initial load
duke
parents:
diff changeset
64
a61af66fc99e Initial load
duke
parents:
diff changeset
65 public boolean isKlassConstant() {
a61af66fc99e Initial load
duke
parents:
diff changeset
66 int jcode = javaCode();
a61af66fc99e Initial load
duke
parents:
diff changeset
67 if (jcode == Bytecodes._ldc2_w) {
a61af66fc99e Initial load
duke
parents:
diff changeset
68 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
69 }
a61af66fc99e Initial load
duke
parents:
diff changeset
70
a61af66fc99e Initial load
duke
parents:
diff changeset
71 ConstantTag ctag = method().getConstants().getTagAt(index());
a61af66fc99e Initial load
duke
parents:
diff changeset
72 return ctag.isKlass() || ctag.isUnresolvedKlass();
a61af66fc99e Initial load
duke
parents:
diff changeset
73 }
a61af66fc99e Initial load
duke
parents:
diff changeset
74
a61af66fc99e Initial load
duke
parents:
diff changeset
75 // return Symbol (if unresolved) or Klass (if resolved)
a61af66fc99e Initial load
duke
parents:
diff changeset
76 public Oop getKlass() {
a61af66fc99e Initial load
duke
parents:
diff changeset
77 if (Assert.ASSERTS_ENABLED) {
a61af66fc99e Initial load
duke
parents:
diff changeset
78 Assert.that(isKlassConstant(), "not a klass literal");
a61af66fc99e Initial load
duke
parents:
diff changeset
79 }
a61af66fc99e Initial load
duke
parents:
diff changeset
80 // tag change from 'unresolved' to 'klass' does not happen atomically.
a61af66fc99e Initial load
duke
parents:
diff changeset
81 // We just look at the object at the corresponding index and
a61af66fc99e Initial load
duke
parents:
diff changeset
82 // decide based on the oop type.
a61af66fc99e Initial load
duke
parents:
diff changeset
83 ConstantPool cpool = method().getConstants();
a61af66fc99e Initial load
duke
parents:
diff changeset
84 int cpIndex = index();
a61af66fc99e Initial load
duke
parents:
diff changeset
85 Oop oop = cpool.getObjAt(cpIndex);
a61af66fc99e Initial load
duke
parents:
diff changeset
86 if (oop.isKlass()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
87 return (Klass) oop;
a61af66fc99e Initial load
duke
parents:
diff changeset
88 } else if (oop.isSymbol()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
89 return (Symbol) oop;
a61af66fc99e Initial load
duke
parents:
diff changeset
90 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
91 throw new RuntimeException("should not reach here");
a61af66fc99e Initial load
duke
parents:
diff changeset
92 }
a61af66fc99e Initial load
duke
parents:
diff changeset
93 }
a61af66fc99e Initial load
duke
parents:
diff changeset
94
a61af66fc99e Initial load
duke
parents:
diff changeset
95 public static BytecodeLoadConstant at(Method method, int bci) {
a61af66fc99e Initial load
duke
parents:
diff changeset
96 BytecodeLoadConstant b = new BytecodeLoadConstant(method, bci);
a61af66fc99e Initial load
duke
parents:
diff changeset
97 if (Assert.ASSERTS_ENABLED) {
a61af66fc99e Initial load
duke
parents:
diff changeset
98 b.verify();
a61af66fc99e Initial load
duke
parents:
diff changeset
99 }
a61af66fc99e Initial load
duke
parents:
diff changeset
100 return b;
a61af66fc99e Initial load
duke
parents:
diff changeset
101 }
a61af66fc99e Initial load
duke
parents:
diff changeset
102
a61af66fc99e Initial load
duke
parents:
diff changeset
103 /** Like at, but returns null if the BCI is not at ldc or ldc_w or ldc2_w */
a61af66fc99e Initial load
duke
parents:
diff changeset
104 public static BytecodeLoadConstant atCheck(Method method, int bci) {
a61af66fc99e Initial load
duke
parents:
diff changeset
105 BytecodeLoadConstant b = new BytecodeLoadConstant(method, bci);
a61af66fc99e Initial load
duke
parents:
diff changeset
106 return (b.isValid() ? b : null);
a61af66fc99e Initial load
duke
parents:
diff changeset
107 }
a61af66fc99e Initial load
duke
parents:
diff changeset
108
a61af66fc99e Initial load
duke
parents:
diff changeset
109 public static BytecodeLoadConstant at(BytecodeStream bcs) {
a61af66fc99e Initial load
duke
parents:
diff changeset
110 return new BytecodeLoadConstant(bcs.method(), bcs.bci());
a61af66fc99e Initial load
duke
parents:
diff changeset
111 }
a61af66fc99e Initial load
duke
parents:
diff changeset
112
a61af66fc99e Initial load
duke
parents:
diff changeset
113 public String getConstantValue() {
a61af66fc99e Initial load
duke
parents:
diff changeset
114 ConstantPool cpool = method().getConstants();
a61af66fc99e Initial load
duke
parents:
diff changeset
115 int cpIndex = index();
a61af66fc99e Initial load
duke
parents:
diff changeset
116 ConstantTag ctag = cpool.getTagAt(cpIndex);
a61af66fc99e Initial load
duke
parents:
diff changeset
117 if (ctag.isInt()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
118 return "<int " + Integer.toString(cpool.getIntAt(cpIndex)) +">";
a61af66fc99e Initial load
duke
parents:
diff changeset
119 } else if (ctag.isLong()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
120 return "<long " + Long.toString(cpool.getLongAt(cpIndex)) + "L>";
a61af66fc99e Initial load
duke
parents:
diff changeset
121 } else if (ctag.isFloat()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
122 return "<float " + Float.toString(cpool.getFloatAt(cpIndex)) + "F>";
a61af66fc99e Initial load
duke
parents:
diff changeset
123 } else if (ctag.isDouble()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
124 return "<double " + Double.toString(cpool.getDoubleAt(cpIndex)) + "D>";
a61af66fc99e Initial load
duke
parents:
diff changeset
125 } else if (ctag.isString() || ctag.isUnresolvedString()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
126 // tag change from 'unresolved' to 'string' does not happen atomically.
a61af66fc99e Initial load
duke
parents:
diff changeset
127 // We just look at the object at the corresponding index and
a61af66fc99e Initial load
duke
parents:
diff changeset
128 // decide based on the oop type.
a61af66fc99e Initial load
duke
parents:
diff changeset
129 Oop obj = cpool.getObjAt(cpIndex);
a61af66fc99e Initial load
duke
parents:
diff changeset
130 if (obj.isSymbol()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
131 Symbol sym = (Symbol) obj;
a61af66fc99e Initial load
duke
parents:
diff changeset
132 return "<String \"" + sym.asString() + "\">";
a61af66fc99e Initial load
duke
parents:
diff changeset
133 } else if (obj.isInstance()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
134 return "<String \"" + OopUtilities.stringOopToString(obj) + "\">";
a61af66fc99e Initial load
duke
parents:
diff changeset
135 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
136 throw new RuntimeException("should not reach here");
a61af66fc99e Initial load
duke
parents:
diff changeset
137 }
a61af66fc99e Initial load
duke
parents:
diff changeset
138 } else if (ctag.isKlass() || ctag.isUnresolvedKlass()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
139 // tag change from 'unresolved' to 'klass' does not happen atomically.
a61af66fc99e Initial load
duke
parents:
diff changeset
140 // We just look at the object at the corresponding index and
a61af66fc99e Initial load
duke
parents:
diff changeset
141 // decide based on the oop type.
a61af66fc99e Initial load
duke
parents:
diff changeset
142 Oop obj = cpool.getObjAt(cpIndex);
a61af66fc99e Initial load
duke
parents:
diff changeset
143 if (obj.isKlass()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
144 Klass k = (Klass) obj;
a61af66fc99e Initial load
duke
parents:
diff changeset
145 return "<Class " + k.getName().asString() + "@" + k.getHandle() + ">";
a61af66fc99e Initial load
duke
parents:
diff changeset
146 } else if (obj.isSymbol()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
147 Symbol sym = (Symbol) obj;
a61af66fc99e Initial load
duke
parents:
diff changeset
148 return "<Class " + sym.asString() + ">";
a61af66fc99e Initial load
duke
parents:
diff changeset
149 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
150 throw new RuntimeException("should not reach here");
a61af66fc99e Initial load
duke
parents:
diff changeset
151 }
a61af66fc99e Initial load
duke
parents:
diff changeset
152 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
153 if (Assert.ASSERTS_ENABLED) {
a61af66fc99e Initial load
duke
parents:
diff changeset
154 Assert.that(false, "invalid load constant type");
a61af66fc99e Initial load
duke
parents:
diff changeset
155 }
a61af66fc99e Initial load
duke
parents:
diff changeset
156 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
157 }
a61af66fc99e Initial load
duke
parents:
diff changeset
158 }
a61af66fc99e Initial load
duke
parents:
diff changeset
159
a61af66fc99e Initial load
duke
parents:
diff changeset
160 public String toString() {
a61af66fc99e Initial load
duke
parents:
diff changeset
161 StringBuffer buf = new StringBuffer();
a61af66fc99e Initial load
duke
parents:
diff changeset
162 buf.append(getJavaBytecodeName());
a61af66fc99e Initial load
duke
parents:
diff changeset
163 buf.append(spaces);
a61af66fc99e Initial load
duke
parents:
diff changeset
164 buf.append('#');
a61af66fc99e Initial load
duke
parents:
diff changeset
165 buf.append(Integer.toString(index()));
a61af66fc99e Initial load
duke
parents:
diff changeset
166 buf.append(spaces);
a61af66fc99e Initial load
duke
parents:
diff changeset
167 buf.append(getConstantValue());
a61af66fc99e Initial load
duke
parents:
diff changeset
168 if (code() != javaCode()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
169 buf.append(spaces);
a61af66fc99e Initial load
duke
parents:
diff changeset
170 buf.append('[');
a61af66fc99e Initial load
duke
parents:
diff changeset
171 buf.append(getBytecodeName());
a61af66fc99e Initial load
duke
parents:
diff changeset
172 buf.append(']');
a61af66fc99e Initial load
duke
parents:
diff changeset
173 }
a61af66fc99e Initial load
duke
parents:
diff changeset
174 return buf.toString();
a61af66fc99e Initial load
duke
parents:
diff changeset
175 }
a61af66fc99e Initial load
duke
parents:
diff changeset
176 }