hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/jcore/ByteCodeRewriter.java
author xdono
Tue, 28 Jul 2009 12:12:40 -0700
changeset 3261 c7d5aae8d3f7
parent 2529 9dfd3cb5648f
child 5547 f4b087cbb361
permissions -rw-r--r--
6862919: Update copyright year Summary: Update copyright for files that have been modified in 2009, up to 07/09 Reviewed-by: tbell, ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
3261
c7d5aae8d3f7 6862919: Update copyright year
xdono
parents: 2529
diff changeset
     2
 * Copyright 2002-2009 Sun Microsystems, Inc.  All Rights Reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
package sun.jvm.hotspot.tools.jcore;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import sun.jvm.hotspot.oops.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
import sun.jvm.hotspot.interpreter.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
import sun.jvm.hotspot.utilities.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
import sun.jvm.hotspot.debugger.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
import sun.jvm.hotspot.runtime.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
public class ByteCodeRewriter
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
    private Method method;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
    private ConstantPool cpool;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
    private ConstantPoolCache cpCache;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
    private byte[] code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
    private Bytes  bytes;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
    public static final boolean DEBUG = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
    private static final int jintSize = 4;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
    protected void debugMessage(String message) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
        System.out.println(message);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
    public ByteCodeRewriter(Method method, ConstantPool cpool, byte[] code) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
        this.method = method;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
        this.cpool = cpool;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
        this.cpCache = cpool.getCache();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
        this.code = code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
        this.bytes = VM.getVM().getBytes();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
    protected short getConstantPoolIndex(int bci) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
       // get ConstantPool index from ConstantPoolCacheIndex at given bci
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
       short cpCacheIndex = method.getBytecodeShortArg(bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
       if (cpCache == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
          return cpCacheIndex;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
       } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
          // change byte-ordering and go via cache
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
          return (short) cpCache.getEntryAt((int) (0xFFFF & bytes.swapShort(cpCacheIndex))).getConstantPoolIndex();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
       }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
    static private void writeShort(byte[] buf, int index, short value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
        buf[index] = (byte) ((value >> 8) & 0x00FF);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
        buf[index + 1] = (byte) (value & 0x00FF);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
    public void rewrite() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
        int bytecode = Bytecodes._illegal;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
        int hotspotcode = Bytecodes._illegal;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
        int len = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
        for (int bci = 0; bci < code.length;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
            hotspotcode = Bytecodes.codeAt(method, bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
            bytecode = Bytecodes.javaCode(hotspotcode);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
            if (Assert.ASSERTS_ENABLED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
                int code_from_buffer = 0xFF & code[bci];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
                Assert.that(code_from_buffer == hotspotcode
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
                          || code_from_buffer == Bytecodes._breakpoint,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
                          "Unexpected bytecode found in method bytecode buffer!");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
            // update the code buffer hotspot specific bytecode with the jvm bytecode
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
            code[bci] = (byte) (0xFF & bytecode);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
            short cpoolIndex = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
            switch (bytecode) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
                // bytecodes with ConstantPoolCache index
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
                case Bytecodes._getstatic:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
                case Bytecodes._putstatic:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
                case Bytecodes._getfield:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
                case Bytecodes._putfield:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
                case Bytecodes._invokevirtual:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
                case Bytecodes._invokespecial:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
                case Bytecodes._invokestatic:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
                case Bytecodes._invokeinterface: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
                    cpoolIndex = getConstantPoolIndex(bci + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
                    writeShort(code, bci + 1, cpoolIndex);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
                    break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
            len = Bytecodes.lengthFor(bytecode);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
            if (len <= 0) len = Bytecodes.lengthAt(method, bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
            if (DEBUG) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
                String operand = "";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
                switch (len) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
                   case 2:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
                        operand += code[bci + 1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
                        break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
                   case 3:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
                        operand += (cpoolIndex != 0)? cpoolIndex :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
                                            method.getBytecodeShortArg(bci + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
                        break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
                   case 5:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
                        operand += method.getBytecodeIntArg(bci + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
                        break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
                // the operand following # is not quite like javap output.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
                // in particular, for goto & goto_w, the operand is PC relative
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
                // offset for jump. Javap adds relative offset with current PC
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
                // to give absolute bci to jump to.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
                String message = "\t\t" + bci + " " + Bytecodes.name(bytecode);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
                if (hotspotcode != bytecode)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
                    message += " [" + Bytecodes.name(hotspotcode) + "]";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
                if (operand != "")
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
                    message += " #" + operand;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
                if (DEBUG) debugMessage(message);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
            bci += len;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
}