Mercurial > hg > graal-jvmci-8
annotate agent/src/share/classes/sun/jvm/hotspot/tools/jcore/ByteCodeRewriter.java @ 14223:de6a9e811145
8029233: Update copyright year to match last edit in jdk8 hotspot repository for 2013
Summary: Copyright year updated for files modified during 2013
Reviewed-by: twisti, iveresov
author | mikael |
---|---|
date | Tue, 24 Dec 2013 11:48:39 -0800 |
parents | c7957b458bf8 |
children | b8a500a7b9bf 64156d22e49d |
rev | line source |
---|---|
0 | 1 /* |
6725
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
2042
diff
changeset
|
2 * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. |
0 | 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 * | |
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 | 22 * |
23 */ | |
24 | |
25 package sun.jvm.hotspot.tools.jcore; | |
26 | |
27 import sun.jvm.hotspot.oops.*; | |
28 import sun.jvm.hotspot.interpreter.*; | |
29 import sun.jvm.hotspot.utilities.*; | |
30 import sun.jvm.hotspot.debugger.*; | |
31 import sun.jvm.hotspot.runtime.*; | |
6880
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
32 import java.security.AccessController; |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
33 import java.security.PrivilegedAction; |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
34 import java.security.AccessControlContext; |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
35 import java.security.PrivilegedExceptionAction; |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
36 import java.security.PrivilegedActionException; |
0 | 37 |
38 public class ByteCodeRewriter | |
39 { | |
40 private Method method; | |
41 private ConstantPool cpool; | |
42 private ConstantPoolCache cpCache; | |
43 private byte[] code; | |
44 private Bytes bytes; | |
45 | |
46 private static final int jintSize = 4; | |
6880
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
47 public static final boolean DEBUG; |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
48 |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
49 static { |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
50 String debug = (String) AccessController.doPrivileged( |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
51 new PrivilegedAction() { |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
52 public Object run() { |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
53 return System.getProperty("sun.jvm.hotspot.tools.jcore.ByteCodeRewriter.DEBUG"); |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
54 } |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
55 } |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
56 ); |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
57 DEBUG = (debug != null ? debug.equalsIgnoreCase("true") : false); |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
58 } |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
59 |
0 | 60 |
61 protected void debugMessage(String message) { | |
62 System.out.println(message); | |
63 } | |
64 | |
65 public ByteCodeRewriter(Method method, ConstantPool cpool, byte[] code) { | |
66 this.method = method; | |
67 this.cpool = cpool; | |
68 this.cpCache = cpool.getCache(); | |
69 this.code = code; | |
70 this.bytes = VM.getVM().getBytes(); | |
71 | |
72 } | |
73 | |
6880
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
74 protected short getConstantPoolIndexFromRefMap(int rawcode, int bci) { |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
75 int refIndex; |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
76 String fmt = Bytecodes.format(rawcode); |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
77 switch (fmt.length()) { |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
78 case 2: refIndex = 0xFF & method.getBytecodeByteArg(bci); break; |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
79 case 3: refIndex = 0xFFFF & bytes.swapShort(method.getBytecodeShortArg(bci)); break; |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
80 default: throw new IllegalArgumentException(); |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
81 } |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
82 |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
83 return (short)cpool.objectToCPIndex(refIndex); |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
84 } |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
85 |
1602 | 86 protected short getConstantPoolIndex(int rawcode, int bci) { |
0 | 87 // get ConstantPool index from ConstantPoolCacheIndex at given bci |
1602 | 88 String fmt = Bytecodes.format(rawcode); |
89 int cpCacheIndex; | |
90 switch (fmt.length()) { | |
91 case 2: cpCacheIndex = method.getBytecodeByteArg(bci); break; | |
92 case 3: cpCacheIndex = method.getBytecodeShortArg(bci); break; | |
93 case 5: | |
94 if (fmt.indexOf("__") >= 0) | |
95 cpCacheIndex = method.getBytecodeShortArg(bci); | |
96 else | |
97 cpCacheIndex = method.getBytecodeIntArg(bci); | |
98 break; | |
99 default: throw new IllegalArgumentException(); | |
100 } | |
0 | 101 if (cpCache == null) { |
1602 | 102 return (short) cpCacheIndex; |
103 } else if (fmt.indexOf("JJJJ") >= 0) { | |
104 // change byte-ordering and go via secondary cache entry | |
6725
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
2042
diff
changeset
|
105 throw new InternalError("unimplemented"); |
1602 | 106 } else if (fmt.indexOf("JJ") >= 0) { |
107 // change byte-ordering and go via cache | |
108 return (short) cpCache.getEntryAt((int) (0xFFFF & bytes.swapShort((short)cpCacheIndex))).getConstantPoolIndex(); | |
109 } else if (fmt.indexOf("j") >= 0) { | |
110 // go via cache | |
111 return (short) cpCache.getEntryAt((int) (0xFF & cpCacheIndex)).getConstantPoolIndex(); | |
0 | 112 } else { |
1602 | 113 return (short) cpCacheIndex; |
0 | 114 } |
115 } | |
116 | |
117 static private void writeShort(byte[] buf, int index, short value) { | |
118 buf[index] = (byte) ((value >> 8) & 0x00FF); | |
119 buf[index + 1] = (byte) (value & 0x00FF); | |
120 } | |
121 | |
122 public void rewrite() { | |
123 int bytecode = Bytecodes._illegal; | |
124 int hotspotcode = Bytecodes._illegal; | |
125 int len = 0; | |
126 | |
6880
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
127 if (DEBUG) { |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
128 String msg = method.getMethodHolder().getName().asString() + "." + |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
129 method.getName().asString() + |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
130 method.getSignature().asString(); |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
131 debugMessage(msg); |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
132 } |
0 | 133 for (int bci = 0; bci < code.length;) { |
134 hotspotcode = Bytecodes.codeAt(method, bci); | |
135 bytecode = Bytecodes.javaCode(hotspotcode); | |
136 | |
137 if (Assert.ASSERTS_ENABLED) { | |
138 int code_from_buffer = 0xFF & code[bci]; | |
139 Assert.that(code_from_buffer == hotspotcode | |
140 || code_from_buffer == Bytecodes._breakpoint, | |
141 "Unexpected bytecode found in method bytecode buffer!"); | |
142 } | |
143 | |
144 // update the code buffer hotspot specific bytecode with the jvm bytecode | |
145 code[bci] = (byte) (0xFF & bytecode); | |
146 | |
147 short cpoolIndex = 0; | |
148 switch (bytecode) { | |
149 // bytecodes with ConstantPoolCache index | |
150 case Bytecodes._getstatic: | |
151 case Bytecodes._putstatic: | |
152 case Bytecodes._getfield: | |
153 case Bytecodes._putfield: | |
154 case Bytecodes._invokevirtual: | |
155 case Bytecodes._invokespecial: | |
156 case Bytecodes._invokestatic: | |
157 case Bytecodes._invokeinterface: { | |
1602 | 158 cpoolIndex = getConstantPoolIndex(hotspotcode, bci + 1); |
0 | 159 writeShort(code, bci + 1, cpoolIndex); |
160 break; | |
161 } | |
1602 | 162 |
163 case Bytecodes._invokedynamic: | |
164 cpoolIndex = getConstantPoolIndex(hotspotcode, bci + 1); | |
165 writeShort(code, bci + 1, cpoolIndex); | |
166 writeShort(code, bci + 3, (short)0); // clear out trailing bytes | |
167 break; | |
168 | |
169 case Bytecodes._ldc_w: | |
170 if (hotspotcode != bytecode) { | |
6880
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
171 // fast_aldc_w puts constant in reference map |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
172 cpoolIndex = getConstantPoolIndexFromRefMap(hotspotcode, bci + 1); |
1602 | 173 writeShort(code, bci + 1, cpoolIndex); |
174 } | |
175 break; | |
176 case Bytecodes._ldc: | |
177 if (hotspotcode != bytecode) { | |
6880
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
178 // fast_aldc puts constant in reference map |
c7957b458bf8
8000818: SA constant pool need to reference to reference map after permgen removal
minqi
parents:
6725
diff
changeset
|
179 cpoolIndex = getConstantPoolIndexFromRefMap(hotspotcode, bci + 1); |
1602 | 180 code[bci + 1] = (byte)(cpoolIndex); |
181 } | |
182 break; | |
0 | 183 } |
184 | |
185 len = Bytecodes.lengthFor(bytecode); | |
186 if (len <= 0) len = Bytecodes.lengthAt(method, bci); | |
187 | |
188 if (DEBUG) { | |
189 String operand = ""; | |
190 switch (len) { | |
191 case 2: | |
192 operand += code[bci + 1]; | |
193 break; | |
194 case 3: | |
195 operand += (cpoolIndex != 0)? cpoolIndex : | |
196 method.getBytecodeShortArg(bci + 1); | |
197 break; | |
198 case 5: | |
199 operand += method.getBytecodeIntArg(bci + 1); | |
200 break; | |
201 } | |
202 | |
203 // the operand following # is not quite like javap output. | |
204 // in particular, for goto & goto_w, the operand is PC relative | |
205 // offset for jump. Javap adds relative offset with current PC | |
206 // to give absolute bci to jump to. | |
207 | |
208 String message = "\t\t" + bci + " " + Bytecodes.name(bytecode); | |
209 if (hotspotcode != bytecode) | |
210 message += " [" + Bytecodes.name(hotspotcode) + "]"; | |
211 if (operand != "") | |
212 message += " #" + operand; | |
213 | |
214 if (DEBUG) debugMessage(message); | |
215 } | |
216 | |
217 bci += len; | |
218 } | |
219 } | |
220 } |