Mercurial > hg > graal-compiler
annotate agent/src/share/classes/sun/jvm/hotspot/tools/jcore/ByteCodeRewriter.java @ 2468:6c97c830fb6f
Merge
author | jrose |
---|---|
date | Sat, 09 Apr 2011 21:16:12 -0700 |
parents | 0a8e0d4345b3 |
children | da91efe96a93 |
rev | line source |
---|---|
0 | 1 /* |
2042
0a8e0d4345b3
7010068: Update all 2010 Oracle-changed OpenJDK files to have the proper copyright dates - first pass
trims
parents:
1602
diff
changeset
|
2 * Copyright (c) 2002, 2010, 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.*; | |
32 | |
33 public class ByteCodeRewriter | |
34 { | |
35 private Method method; | |
36 private ConstantPool cpool; | |
37 private ConstantPoolCache cpCache; | |
38 private byte[] code; | |
39 private Bytes bytes; | |
40 | |
41 public static final boolean DEBUG = false; | |
42 private static final int jintSize = 4; | |
43 | |
44 protected void debugMessage(String message) { | |
45 System.out.println(message); | |
46 } | |
47 | |
48 public ByteCodeRewriter(Method method, ConstantPool cpool, byte[] code) { | |
49 this.method = method; | |
50 this.cpool = cpool; | |
51 this.cpCache = cpool.getCache(); | |
52 this.code = code; | |
53 this.bytes = VM.getVM().getBytes(); | |
54 | |
55 } | |
56 | |
1602 | 57 protected short getConstantPoolIndex(int rawcode, int bci) { |
0 | 58 // get ConstantPool index from ConstantPoolCacheIndex at given bci |
1602 | 59 String fmt = Bytecodes.format(rawcode); |
60 int cpCacheIndex; | |
61 switch (fmt.length()) { | |
62 case 2: cpCacheIndex = method.getBytecodeByteArg(bci); break; | |
63 case 3: cpCacheIndex = method.getBytecodeShortArg(bci); break; | |
64 case 5: | |
65 if (fmt.indexOf("__") >= 0) | |
66 cpCacheIndex = method.getBytecodeShortArg(bci); | |
67 else | |
68 cpCacheIndex = method.getBytecodeIntArg(bci); | |
69 break; | |
70 default: throw new IllegalArgumentException(); | |
71 } | |
0 | 72 if (cpCache == null) { |
1602 | 73 return (short) cpCacheIndex; |
74 } else if (fmt.indexOf("JJJJ") >= 0) { | |
75 // change byte-ordering and go via secondary cache entry | |
76 return (short) cpCache.getMainEntryAt(bytes.swapInt(cpCacheIndex)).getConstantPoolIndex(); | |
77 } else if (fmt.indexOf("JJ") >= 0) { | |
78 // change byte-ordering and go via cache | |
79 return (short) cpCache.getEntryAt((int) (0xFFFF & bytes.swapShort((short)cpCacheIndex))).getConstantPoolIndex(); | |
80 } else if (fmt.indexOf("j") >= 0) { | |
81 // go via cache | |
82 return (short) cpCache.getEntryAt((int) (0xFF & cpCacheIndex)).getConstantPoolIndex(); | |
0 | 83 } else { |
1602 | 84 return (short) cpCacheIndex; |
0 | 85 } |
86 } | |
87 | |
88 static private void writeShort(byte[] buf, int index, short value) { | |
89 buf[index] = (byte) ((value >> 8) & 0x00FF); | |
90 buf[index + 1] = (byte) (value & 0x00FF); | |
91 } | |
92 | |
93 public void rewrite() { | |
94 int bytecode = Bytecodes._illegal; | |
95 int hotspotcode = Bytecodes._illegal; | |
96 int len = 0; | |
97 | |
98 for (int bci = 0; bci < code.length;) { | |
99 hotspotcode = Bytecodes.codeAt(method, bci); | |
100 bytecode = Bytecodes.javaCode(hotspotcode); | |
101 | |
102 if (Assert.ASSERTS_ENABLED) { | |
103 int code_from_buffer = 0xFF & code[bci]; | |
104 Assert.that(code_from_buffer == hotspotcode | |
105 || code_from_buffer == Bytecodes._breakpoint, | |
106 "Unexpected bytecode found in method bytecode buffer!"); | |
107 } | |
108 | |
109 // update the code buffer hotspot specific bytecode with the jvm bytecode | |
110 code[bci] = (byte) (0xFF & bytecode); | |
111 | |
112 short cpoolIndex = 0; | |
113 switch (bytecode) { | |
114 // bytecodes with ConstantPoolCache index | |
115 case Bytecodes._getstatic: | |
116 case Bytecodes._putstatic: | |
117 case Bytecodes._getfield: | |
118 case Bytecodes._putfield: | |
119 case Bytecodes._invokevirtual: | |
120 case Bytecodes._invokespecial: | |
121 case Bytecodes._invokestatic: | |
122 case Bytecodes._invokeinterface: { | |
1602 | 123 cpoolIndex = getConstantPoolIndex(hotspotcode, bci + 1); |
0 | 124 writeShort(code, bci + 1, cpoolIndex); |
125 break; | |
126 } | |
1602 | 127 |
128 case Bytecodes._invokedynamic: | |
129 cpoolIndex = getConstantPoolIndex(hotspotcode, bci + 1); | |
130 writeShort(code, bci + 1, cpoolIndex); | |
131 writeShort(code, bci + 3, (short)0); // clear out trailing bytes | |
132 break; | |
133 | |
134 case Bytecodes._ldc_w: | |
135 if (hotspotcode != bytecode) { | |
136 // fast_aldc_w puts constant in CP cache | |
137 cpoolIndex = getConstantPoolIndex(hotspotcode, bci + 1); | |
138 writeShort(code, bci + 1, cpoolIndex); | |
139 } | |
140 break; | |
141 case Bytecodes._ldc: | |
142 if (hotspotcode != bytecode) { | |
143 // fast_aldc puts constant in CP cache | |
144 cpoolIndex = getConstantPoolIndex(hotspotcode, bci + 1); | |
145 code[bci + 1] = (byte)(cpoolIndex); | |
146 } | |
147 break; | |
0 | 148 } |
149 | |
150 len = Bytecodes.lengthFor(bytecode); | |
151 if (len <= 0) len = Bytecodes.lengthAt(method, bci); | |
152 | |
153 if (DEBUG) { | |
154 String operand = ""; | |
155 switch (len) { | |
156 case 2: | |
157 operand += code[bci + 1]; | |
158 break; | |
159 case 3: | |
160 operand += (cpoolIndex != 0)? cpoolIndex : | |
161 method.getBytecodeShortArg(bci + 1); | |
162 break; | |
163 case 5: | |
164 operand += method.getBytecodeIntArg(bci + 1); | |
165 break; | |
166 } | |
167 | |
168 // the operand following # is not quite like javap output. | |
169 // in particular, for goto & goto_w, the operand is PC relative | |
170 // offset for jump. Javap adds relative offset with current PC | |
171 // to give absolute bci to jump to. | |
172 | |
173 String message = "\t\t" + bci + " " + Bytecodes.name(bytecode); | |
174 if (hotspotcode != bytecode) | |
175 message += " [" + Bytecodes.name(hotspotcode) + "]"; | |
176 if (operand != "") | |
177 message += " #" + operand; | |
178 | |
179 if (DEBUG) debugMessage(message); | |
180 } | |
181 | |
182 bci += len; | |
183 } | |
184 } | |
185 } |