annotate agent/src/share/classes/sun/jvm/hotspot/tools/jcore/ByteCodeRewriter.java @ 844:bd02caa94611

6862919: Update copyright year Summary: Update copyright for files that have been modified in 2009, up to 07/09 Reviewed-by: tbell, ohair
author xdono
date Tue, 28 Jul 2009 12:12:40 -0700
parents 1f2abec69714
children c18cbe5936b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
844
bd02caa94611 6862919: Update copyright year
xdono
parents: 705
diff changeset
2 * Copyright 2002-2009 Sun Microsystems, Inc. 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 *
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.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
a61af66fc99e Initial load
duke
parents:
diff changeset
57 protected short getConstantPoolIndex(int bci) {
a61af66fc99e Initial load
duke
parents:
diff changeset
58 // get ConstantPool index from ConstantPoolCacheIndex at given bci
a61af66fc99e Initial load
duke
parents:
diff changeset
59 short cpCacheIndex = method.getBytecodeShortArg(bci);
a61af66fc99e Initial load
duke
parents:
diff changeset
60 if (cpCache == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
61 return cpCacheIndex;
a61af66fc99e Initial load
duke
parents:
diff changeset
62 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
63 // change byte-ordering and go via cache
a61af66fc99e Initial load
duke
parents:
diff changeset
64 return (short) cpCache.getEntryAt((int) (0xFFFF & bytes.swapShort(cpCacheIndex))).getConstantPoolIndex();
a61af66fc99e Initial load
duke
parents:
diff changeset
65 }
a61af66fc99e Initial load
duke
parents:
diff changeset
66 }
a61af66fc99e Initial load
duke
parents:
diff changeset
67
a61af66fc99e Initial load
duke
parents:
diff changeset
68 static private void writeShort(byte[] buf, int index, short value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
69 buf[index] = (byte) ((value >> 8) & 0x00FF);
a61af66fc99e Initial load
duke
parents:
diff changeset
70 buf[index + 1] = (byte) (value & 0x00FF);
a61af66fc99e Initial load
duke
parents:
diff changeset
71 }
a61af66fc99e Initial load
duke
parents:
diff changeset
72
a61af66fc99e Initial load
duke
parents:
diff changeset
73 public void rewrite() {
a61af66fc99e Initial load
duke
parents:
diff changeset
74 int bytecode = Bytecodes._illegal;
a61af66fc99e Initial load
duke
parents:
diff changeset
75 int hotspotcode = Bytecodes._illegal;
a61af66fc99e Initial load
duke
parents:
diff changeset
76 int len = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
77
a61af66fc99e Initial load
duke
parents:
diff changeset
78 for (int bci = 0; bci < code.length;) {
a61af66fc99e Initial load
duke
parents:
diff changeset
79 hotspotcode = Bytecodes.codeAt(method, bci);
a61af66fc99e Initial load
duke
parents:
diff changeset
80 bytecode = Bytecodes.javaCode(hotspotcode);
a61af66fc99e Initial load
duke
parents:
diff changeset
81
a61af66fc99e Initial load
duke
parents:
diff changeset
82 if (Assert.ASSERTS_ENABLED) {
a61af66fc99e Initial load
duke
parents:
diff changeset
83 int code_from_buffer = 0xFF & code[bci];
a61af66fc99e Initial load
duke
parents:
diff changeset
84 Assert.that(code_from_buffer == hotspotcode
a61af66fc99e Initial load
duke
parents:
diff changeset
85 || code_from_buffer == Bytecodes._breakpoint,
a61af66fc99e Initial load
duke
parents:
diff changeset
86 "Unexpected bytecode found in method bytecode buffer!");
a61af66fc99e Initial load
duke
parents:
diff changeset
87 }
a61af66fc99e Initial load
duke
parents:
diff changeset
88
a61af66fc99e Initial load
duke
parents:
diff changeset
89 // update the code buffer hotspot specific bytecode with the jvm bytecode
a61af66fc99e Initial load
duke
parents:
diff changeset
90 code[bci] = (byte) (0xFF & bytecode);
a61af66fc99e Initial load
duke
parents:
diff changeset
91
a61af66fc99e Initial load
duke
parents:
diff changeset
92 short cpoolIndex = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
93 switch (bytecode) {
a61af66fc99e Initial load
duke
parents:
diff changeset
94 // bytecodes with ConstantPoolCache index
a61af66fc99e Initial load
duke
parents:
diff changeset
95 case Bytecodes._getstatic:
a61af66fc99e Initial load
duke
parents:
diff changeset
96 case Bytecodes._putstatic:
a61af66fc99e Initial load
duke
parents:
diff changeset
97 case Bytecodes._getfield:
a61af66fc99e Initial load
duke
parents:
diff changeset
98 case Bytecodes._putfield:
a61af66fc99e Initial load
duke
parents:
diff changeset
99 case Bytecodes._invokevirtual:
a61af66fc99e Initial load
duke
parents:
diff changeset
100 case Bytecodes._invokespecial:
a61af66fc99e Initial load
duke
parents:
diff changeset
101 case Bytecodes._invokestatic:
a61af66fc99e Initial load
duke
parents:
diff changeset
102 case Bytecodes._invokeinterface: {
a61af66fc99e Initial load
duke
parents:
diff changeset
103 cpoolIndex = getConstantPoolIndex(bci + 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
104 writeShort(code, bci + 1, cpoolIndex);
a61af66fc99e Initial load
duke
parents:
diff changeset
105 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
106 }
a61af66fc99e Initial load
duke
parents:
diff changeset
107 }
a61af66fc99e Initial load
duke
parents:
diff changeset
108
a61af66fc99e Initial load
duke
parents:
diff changeset
109 len = Bytecodes.lengthFor(bytecode);
a61af66fc99e Initial load
duke
parents:
diff changeset
110 if (len <= 0) len = Bytecodes.lengthAt(method, bci);
a61af66fc99e Initial load
duke
parents:
diff changeset
111
a61af66fc99e Initial load
duke
parents:
diff changeset
112 if (DEBUG) {
a61af66fc99e Initial load
duke
parents:
diff changeset
113 String operand = "";
a61af66fc99e Initial load
duke
parents:
diff changeset
114 switch (len) {
a61af66fc99e Initial load
duke
parents:
diff changeset
115 case 2:
a61af66fc99e Initial load
duke
parents:
diff changeset
116 operand += code[bci + 1];
a61af66fc99e Initial load
duke
parents:
diff changeset
117 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
118 case 3:
a61af66fc99e Initial load
duke
parents:
diff changeset
119 operand += (cpoolIndex != 0)? cpoolIndex :
a61af66fc99e Initial load
duke
parents:
diff changeset
120 method.getBytecodeShortArg(bci + 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
121 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
122 case 5:
a61af66fc99e Initial load
duke
parents:
diff changeset
123 operand += method.getBytecodeIntArg(bci + 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
124 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
125 }
a61af66fc99e Initial load
duke
parents:
diff changeset
126
a61af66fc99e Initial load
duke
parents:
diff changeset
127 // the operand following # is not quite like javap output.
a61af66fc99e Initial load
duke
parents:
diff changeset
128 // in particular, for goto & goto_w, the operand is PC relative
a61af66fc99e Initial load
duke
parents:
diff changeset
129 // offset for jump. Javap adds relative offset with current PC
a61af66fc99e Initial load
duke
parents:
diff changeset
130 // to give absolute bci to jump to.
a61af66fc99e Initial load
duke
parents:
diff changeset
131
a61af66fc99e Initial load
duke
parents:
diff changeset
132 String message = "\t\t" + bci + " " + Bytecodes.name(bytecode);
a61af66fc99e Initial load
duke
parents:
diff changeset
133 if (hotspotcode != bytecode)
a61af66fc99e Initial load
duke
parents:
diff changeset
134 message += " [" + Bytecodes.name(hotspotcode) + "]";
a61af66fc99e Initial load
duke
parents:
diff changeset
135 if (operand != "")
a61af66fc99e Initial load
duke
parents:
diff changeset
136 message += " #" + operand;
a61af66fc99e Initial load
duke
parents:
diff changeset
137
a61af66fc99e Initial load
duke
parents:
diff changeset
138 if (DEBUG) debugMessage(message);
a61af66fc99e Initial load
duke
parents:
diff changeset
139 }
a61af66fc99e Initial load
duke
parents:
diff changeset
140
a61af66fc99e Initial load
duke
parents:
diff changeset
141 bci += len;
a61af66fc99e Initial load
duke
parents:
diff changeset
142 }
a61af66fc99e Initial load
duke
parents:
diff changeset
143 }
a61af66fc99e Initial load
duke
parents:
diff changeset
144 }