1 /*
2  * Copyright (c) 2002, 2017, 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 sun.jvm.hotspot.interpreter;
26 
27 import sun.jvm.hotspot.oops.*;
28 import sun.jvm.hotspot.runtime.*;
29 import sun.jvm.hotspot.utilities.*;
30 
31 public class BytecodeLoadConstant extends Bytecode {
BytecodeLoadConstant(Method method, int bci)32   BytecodeLoadConstant(Method method, int bci) {
33     super(method, bci);
34   }
35 
hasCacheIndex()36   public boolean hasCacheIndex() {
37     // normal ldc uses CP index, but fast_aldc uses swapped CP cache index
38     return code() >= Bytecodes.number_of_java_codes;
39   }
40 
rawIndex()41   int rawIndex() {
42     if (javaCode() == Bytecodes._ldc)
43       return getIndexU1();
44     else
45       return getIndexU2(code(), false);
46   }
47 
poolIndex()48   public int poolIndex() {
49     int index = rawIndex();
50     if (hasCacheIndex()) {
51       return method().getConstants().objectToCPIndex(index);
52     } else {
53       return index;
54     }
55   }
56 
cacheIndex()57   public int cacheIndex() {
58     if (hasCacheIndex()) {
59       return rawIndex();
60     } else {
61       return -1;  // no cache index
62     }
63   }
64 
resultType()65   public BasicType resultType() {
66     int index = poolIndex();
67     ConstantTag tag = method().getConstants().getTagAt(index);
68     return tag.basicType();
69   }
70 
getCachedConstant()71   private Oop getCachedConstant() {
72     int i = cacheIndex();
73     if (i >= 0) {
74       throw new InternalError("invokedynamic not implemented yet");
75     }
76     return null;
77   }
78 
verify()79   public void verify() {
80     if (Assert.ASSERTS_ENABLED) {
81       Assert.that(isValid(), "check load constant");
82     }
83   }
84 
isValid()85   public boolean isValid() {
86     int jcode = javaCode();
87     boolean codeOk = jcode == Bytecodes._ldc || jcode == Bytecodes._ldc_w ||
88            jcode == Bytecodes._ldc2_w;
89     if (! codeOk) return false;
90 
91     ConstantTag ctag = method().getConstants().getTagAt(poolIndex());
92     if (jcode == Bytecodes._ldc2_w) {
93        // has to be double or long
94        return (ctag.isDouble() || ctag.isLong()) ? true: false;
95     } else {
96        // has to be int or float or String or Klass
97        return (ctag.isString()
98                || ctag.isUnresolvedKlass() || ctag.isKlass()
99                || ctag.isMethodHandle() || ctag.isMethodType()
100                || ctag.isInt() || ctag.isFloat())? true: false;
101     }
102   }
103 
isKlassConstant()104   public boolean isKlassConstant() {
105     int jcode = javaCode();
106     if (jcode == Bytecodes._ldc2_w) {
107        return false;
108     }
109 
110     ConstantTag ctag = method().getConstants().getTagAt(poolIndex());
111     return ctag.isKlass() || ctag.isUnresolvedKlass();
112   }
113 
114   // return Symbol (if unresolved) or Klass (if resolved)
getKlass()115   public Object getKlass() {
116     if (Assert.ASSERTS_ENABLED) {
117       Assert.that(isKlassConstant(), "not a klass literal");
118     }
119     // tag change from 'unresolved' to 'klass' does not happen atomically.
120     // We just look at the object at the corresponding index and
121     // decide based on the oop type.
122     ConstantPool cpool = method().getConstants();
123     int cpIndex = poolIndex();
124     return cpool.getKlassNameAt(cpIndex);
125   }
126 
at(Method method, int bci)127   public static BytecodeLoadConstant at(Method method, int bci) {
128     BytecodeLoadConstant b = new BytecodeLoadConstant(method, bci);
129     if (Assert.ASSERTS_ENABLED) {
130       b.verify();
131     }
132     return b;
133   }
134 
135   /** Like at, but returns null if the BCI is not at ldc or ldc_w or ldc2_w  */
atCheck(Method method, int bci)136   public static BytecodeLoadConstant atCheck(Method method, int bci) {
137     BytecodeLoadConstant b = new BytecodeLoadConstant(method, bci);
138     return (b.isValid() ? b : null);
139   }
140 
at(BytecodeStream bcs)141   public static BytecodeLoadConstant at(BytecodeStream bcs) {
142     return new BytecodeLoadConstant(bcs.method(), bcs.bci());
143   }
144 
getConstantValue()145   public String getConstantValue() {
146     ConstantPool cpool = method().getConstants();
147     int cpIndex = poolIndex();
148     ConstantTag ctag = cpool.getTagAt(cpIndex);
149     if (ctag.isInt()) {
150        return "<int " + Integer.toString(cpool.getIntAt(cpIndex)) +">";
151     } else if (ctag.isLong()) {
152        return "<long " + Long.toString(cpool.getLongAt(cpIndex)) + "L>";
153     } else if (ctag.isFloat()) {
154        return "<float " + Float.toString(cpool.getFloatAt(cpIndex)) + "F>";
155     } else if (ctag.isDouble()) {
156        return "<double " + Double.toString(cpool.getDoubleAt(cpIndex)) + "D>";
157     } else if (ctag.isString()) {
158        // tag change from 'unresolved' to 'string' does not happen atomically.
159        // We just look at the object at the corresponding index and
160        // decide based on the oop type.
161        Symbol sym = cpool.getUnresolvedStringAt(cpIndex);
162          return "<String \"" + sym.asString() + "\">";
163     } else if (ctag.isKlass() || ctag.isUnresolvedKlass()) {
164        // tag change from 'unresolved' to 'klass' does not happen atomically.
165        // We just look at the object at the corresponding index and
166        // decide based on the oop type.
167        ConstantTag tag = cpool.getTagAt(cpIndex);
168        if (tag.isKlass()) {
169          Klass k = cpool.getKlassAt(cpIndex);
170          return "<Class " + k.getName().asString() + "@" + k.getAddress() + ">";
171        } else if (tag.isUnresolvedKlass()) {
172          Symbol sym = cpool.getKlassNameAt(cpIndex);
173          return "<Class " + sym.asString() + ">";
174        } else {
175           throw new RuntimeException("should not reach here");
176        }
177     } else if (ctag.isMethodHandle()) {
178        Oop x = getCachedConstant();
179        int refidx = cpool.getMethodHandleIndexAt(cpIndex);
180        int refkind = cpool.getMethodHandleRefKindAt(cpIndex);
181        return "<MethodHandle kind=" + Integer.toString(refkind) +
182            " ref=" + Integer.toString(refidx)
183            + (x == null ? "" : " @" + x.getHandle()) + ">";
184     } else if (ctag.isMethodType()) {
185        Oop x = getCachedConstant();
186        int refidx = cpool.getMethodTypeIndexAt(cpIndex);
187        return "<MethodType " + cpool.getSymbolAt(refidx).asString()
188            + (x == null ? "" : " @" + x.getHandle()) + ">";
189     } else {
190        if (Assert.ASSERTS_ENABLED) {
191          Assert.that(false, "invalid load constant type");
192        }
193        return null;
194     }
195   }
196 
toString()197   public String toString() {
198     StringBuffer buf = new StringBuffer();
199     buf.append(getJavaBytecodeName());
200     buf.append(spaces);
201     buf.append('#');
202     buf.append(Integer.toString(poolIndex()));
203     if (hasCacheIndex()) {
204        buf.append('(');
205        buf.append(Integer.toString(cacheIndex()));
206        buf.append(')');
207     }
208     buf.append(spaces);
209     buf.append(getConstantValue());
210     if (code() != javaCode()) {
211        buf.append(spaces);
212        buf.append('[');
213        buf.append(getBytecodeName());
214        buf.append(']');
215     }
216     return buf.toString();
217   }
218 }
219