annotate agent/src/share/classes/sun/jvm/hotspot/tools/jcore/ByteCodeRewriter.java @ 1602:136b78722a08

6939203: JSR 292 needs method handle constants Summary: Add new CP types CONSTANT_MethodHandle, CONSTANT_MethodType; extend 'ldc' bytecode. Reviewed-by: twisti, never
author jrose
date Wed, 09 Jun 2010 18:50:45 -0700
parents c18cbe5936b8
children 0a8e0d4345b3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 844
diff changeset
2 * Copyright (c) 2002, 2009, Oracle and/or its affiliates. All rights reserved.
0
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 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 844
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 844
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 844
diff changeset
21 * questions.
0
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.tools.jcore;
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.interpreter.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
29 import sun.jvm.hotspot.utilities.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 import sun.jvm.hotspot.debugger.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
31 import sun.jvm.hotspot.runtime.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
32
a61af66fc99e Initial load
duke
parents:
diff changeset
33 public class ByteCodeRewriter
a61af66fc99e Initial load
duke
parents:
diff changeset
34 {
a61af66fc99e Initial load
duke
parents:
diff changeset
35 private Method method;
a61af66fc99e Initial load
duke
parents:
diff changeset
36 private ConstantPool cpool;
a61af66fc99e Initial load
duke
parents:
diff changeset
37 private ConstantPoolCache cpCache;
a61af66fc99e Initial load
duke
parents:
diff changeset
38 private byte[] code;
a61af66fc99e Initial load
duke
parents:
diff changeset
39 private Bytes bytes;
a61af66fc99e Initial load
duke
parents:
diff changeset
40
a61af66fc99e Initial load
duke
parents:
diff changeset
41 public static final boolean DEBUG = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
42 private static final int jintSize = 4;
a61af66fc99e Initial load
duke
parents:
diff changeset
43
a61af66fc99e Initial load
duke
parents:
diff changeset
44 protected void debugMessage(String message) {
a61af66fc99e Initial load
duke
parents:
diff changeset
45 System.out.println(message);
a61af66fc99e Initial load
duke
parents:
diff changeset
46 }
a61af66fc99e Initial load
duke
parents:
diff changeset
47
a61af66fc99e Initial load
duke
parents:
diff changeset
48 public ByteCodeRewriter(Method method, ConstantPool cpool, byte[] code) {
a61af66fc99e Initial load
duke
parents:
diff changeset
49 this.method = method;
a61af66fc99e Initial load
duke
parents:
diff changeset
50 this.cpool = cpool;
a61af66fc99e Initial load
duke
parents:
diff changeset
51 this.cpCache = cpool.getCache();
a61af66fc99e Initial load
duke
parents:
diff changeset
52 this.code = code;
a61af66fc99e Initial load
duke
parents:
diff changeset
53 this.bytes = VM.getVM().getBytes();
a61af66fc99e Initial load
duke
parents:
diff changeset
54
a61af66fc99e Initial load
duke
parents:
diff changeset
55 }
a61af66fc99e Initial load
duke
parents:
diff changeset
56
1602
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
57 protected short getConstantPoolIndex(int rawcode, int bci) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
58 // get ConstantPool index from ConstantPoolCacheIndex at given bci
1602
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
59 String fmt = Bytecodes.format(rawcode);
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
60 int cpCacheIndex;
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
61 switch (fmt.length()) {
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
62 case 2: cpCacheIndex = method.getBytecodeByteArg(bci); break;
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
63 case 3: cpCacheIndex = method.getBytecodeShortArg(bci); break;
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
64 case 5:
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
65 if (fmt.indexOf("__") >= 0)
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
66 cpCacheIndex = method.getBytecodeShortArg(bci);
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
67 else
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
68 cpCacheIndex = method.getBytecodeIntArg(bci);
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
69 break;
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
70 default: throw new IllegalArgumentException();
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
71 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
72 if (cpCache == null) {
1602
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
73 return (short) cpCacheIndex;
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
74 } else if (fmt.indexOf("JJJJ") >= 0) {
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
75 // change byte-ordering and go via secondary cache entry
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
76 return (short) cpCache.getMainEntryAt(bytes.swapInt(cpCacheIndex)).getConstantPoolIndex();
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
77 } else if (fmt.indexOf("JJ") >= 0) {
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
78 // change byte-ordering and go via cache
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
79 return (short) cpCache.getEntryAt((int) (0xFFFF & bytes.swapShort((short)cpCacheIndex))).getConstantPoolIndex();
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
80 } else if (fmt.indexOf("j") >= 0) {
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
81 // go via cache
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
82 return (short) cpCache.getEntryAt((int) (0xFF & cpCacheIndex)).getConstantPoolIndex();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
83 } else {
1602
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
84 return (short) cpCacheIndex;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
85 }
a61af66fc99e Initial load
duke
parents:
diff changeset
86 }
a61af66fc99e Initial load
duke
parents:
diff changeset
87
a61af66fc99e Initial load
duke
parents:
diff changeset
88 static private void writeShort(byte[] buf, int index, short value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
89 buf[index] = (byte) ((value >> 8) & 0x00FF);
a61af66fc99e Initial load
duke
parents:
diff changeset
90 buf[index + 1] = (byte) (value & 0x00FF);
a61af66fc99e Initial load
duke
parents:
diff changeset
91 }
a61af66fc99e Initial load
duke
parents:
diff changeset
92
a61af66fc99e Initial load
duke
parents:
diff changeset
93 public void rewrite() {
a61af66fc99e Initial load
duke
parents:
diff changeset
94 int bytecode = Bytecodes._illegal;
a61af66fc99e Initial load
duke
parents:
diff changeset
95 int hotspotcode = Bytecodes._illegal;
a61af66fc99e Initial load
duke
parents:
diff changeset
96 int len = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
97
a61af66fc99e Initial load
duke
parents:
diff changeset
98 for (int bci = 0; bci < code.length;) {
a61af66fc99e Initial load
duke
parents:
diff changeset
99 hotspotcode = Bytecodes.codeAt(method, bci);
a61af66fc99e Initial load
duke
parents:
diff changeset
100 bytecode = Bytecodes.javaCode(hotspotcode);
a61af66fc99e Initial load
duke
parents:
diff changeset
101
a61af66fc99e Initial load
duke
parents:
diff changeset
102 if (Assert.ASSERTS_ENABLED) {
a61af66fc99e Initial load
duke
parents:
diff changeset
103 int code_from_buffer = 0xFF & code[bci];
a61af66fc99e Initial load
duke
parents:
diff changeset
104 Assert.that(code_from_buffer == hotspotcode
a61af66fc99e Initial load
duke
parents:
diff changeset
105 || code_from_buffer == Bytecodes._breakpoint,
a61af66fc99e Initial load
duke
parents:
diff changeset
106 "Unexpected bytecode found in method bytecode buffer!");
a61af66fc99e Initial load
duke
parents:
diff changeset
107 }
a61af66fc99e Initial load
duke
parents:
diff changeset
108
a61af66fc99e Initial load
duke
parents:
diff changeset
109 // update the code buffer hotspot specific bytecode with the jvm bytecode
a61af66fc99e Initial load
duke
parents:
diff changeset
110 code[bci] = (byte) (0xFF & bytecode);
a61af66fc99e Initial load
duke
parents:
diff changeset
111
a61af66fc99e Initial load
duke
parents:
diff changeset
112 short cpoolIndex = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
113 switch (bytecode) {
a61af66fc99e Initial load
duke
parents:
diff changeset
114 // bytecodes with ConstantPoolCache index
a61af66fc99e Initial load
duke
parents:
diff changeset
115 case Bytecodes._getstatic:
a61af66fc99e Initial load
duke
parents:
diff changeset
116 case Bytecodes._putstatic:
a61af66fc99e Initial load
duke
parents:
diff changeset
117 case Bytecodes._getfield:
a61af66fc99e Initial load
duke
parents:
diff changeset
118 case Bytecodes._putfield:
a61af66fc99e Initial load
duke
parents:
diff changeset
119 case Bytecodes._invokevirtual:
a61af66fc99e Initial load
duke
parents:
diff changeset
120 case Bytecodes._invokespecial:
a61af66fc99e Initial load
duke
parents:
diff changeset
121 case Bytecodes._invokestatic:
a61af66fc99e Initial load
duke
parents:
diff changeset
122 case Bytecodes._invokeinterface: {
1602
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
123 cpoolIndex = getConstantPoolIndex(hotspotcode, bci + 1);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
124 writeShort(code, bci + 1, cpoolIndex);
a61af66fc99e Initial load
duke
parents:
diff changeset
125 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
126 }
1602
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
127
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
128 case Bytecodes._invokedynamic:
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
129 cpoolIndex = getConstantPoolIndex(hotspotcode, bci + 1);
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
130 writeShort(code, bci + 1, cpoolIndex);
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
131 writeShort(code, bci + 3, (short)0); // clear out trailing bytes
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
132 break;
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
133
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
134 case Bytecodes._ldc_w:
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
135 if (hotspotcode != bytecode) {
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
136 // fast_aldc_w puts constant in CP cache
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
137 cpoolIndex = getConstantPoolIndex(hotspotcode, bci + 1);
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
138 writeShort(code, bci + 1, cpoolIndex);
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
139 }
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
140 break;
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
141 case Bytecodes._ldc:
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
142 if (hotspotcode != bytecode) {
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
143 // fast_aldc puts constant in CP cache
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
144 cpoolIndex = getConstantPoolIndex(hotspotcode, bci + 1);
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
145 code[bci + 1] = (byte)(cpoolIndex);
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
146 }
136b78722a08 6939203: JSR 292 needs method handle constants
jrose
parents: 1552
diff changeset
147 break;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
148 }
a61af66fc99e Initial load
duke
parents:
diff changeset
149
a61af66fc99e Initial load
duke
parents:
diff changeset
150 len = Bytecodes.lengthFor(bytecode);
a61af66fc99e Initial load
duke
parents:
diff changeset
151 if (len <= 0) len = Bytecodes.lengthAt(method, bci);
a61af66fc99e Initial load
duke
parents:
diff changeset
152
a61af66fc99e Initial load
duke
parents:
diff changeset
153 if (DEBUG) {
a61af66fc99e Initial load
duke
parents:
diff changeset
154 String operand = "";
a61af66fc99e Initial load
duke
parents:
diff changeset
155 switch (len) {
a61af66fc99e Initial load
duke
parents:
diff changeset
156 case 2:
a61af66fc99e Initial load
duke
parents:
diff changeset
157 operand += code[bci + 1];
a61af66fc99e Initial load
duke
parents:
diff changeset
158 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
159 case 3:
a61af66fc99e Initial load
duke
parents:
diff changeset
160 operand += (cpoolIndex != 0)? cpoolIndex :
a61af66fc99e Initial load
duke
parents:
diff changeset
161 method.getBytecodeShortArg(bci + 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
162 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
163 case 5:
a61af66fc99e Initial load
duke
parents:
diff changeset
164 operand += method.getBytecodeIntArg(bci + 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
165 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
166 }
a61af66fc99e Initial load
duke
parents:
diff changeset
167
a61af66fc99e Initial load
duke
parents:
diff changeset
168 // the operand following # is not quite like javap output.
a61af66fc99e Initial load
duke
parents:
diff changeset
169 // in particular, for goto & goto_w, the operand is PC relative
a61af66fc99e Initial load
duke
parents:
diff changeset
170 // offset for jump. Javap adds relative offset with current PC
a61af66fc99e Initial load
duke
parents:
diff changeset
171 // to give absolute bci to jump to.
a61af66fc99e Initial load
duke
parents:
diff changeset
172
a61af66fc99e Initial load
duke
parents:
diff changeset
173 String message = "\t\t" + bci + " " + Bytecodes.name(bytecode);
a61af66fc99e Initial load
duke
parents:
diff changeset
174 if (hotspotcode != bytecode)
a61af66fc99e Initial load
duke
parents:
diff changeset
175 message += " [" + Bytecodes.name(hotspotcode) + "]";
a61af66fc99e Initial load
duke
parents:
diff changeset
176 if (operand != "")
a61af66fc99e Initial load
duke
parents:
diff changeset
177 message += " #" + operand;
a61af66fc99e Initial load
duke
parents:
diff changeset
178
a61af66fc99e Initial load
duke
parents:
diff changeset
179 if (DEBUG) debugMessage(message);
a61af66fc99e Initial load
duke
parents:
diff changeset
180 }
a61af66fc99e Initial load
duke
parents:
diff changeset
181
a61af66fc99e Initial load
duke
parents:
diff changeset
182 bci += len;
a61af66fc99e Initial load
duke
parents:
diff changeset
183 }
a61af66fc99e Initial load
duke
parents:
diff changeset
184 }
a61af66fc99e Initial load
duke
parents:
diff changeset
185 }