hotspot/agent/src/share/classes/sun/jvm/hotspot/interpreter/BytecodeDisassembler.java
author jrose
Wed, 09 Jun 2010 18:50:45 -0700
changeset 5882 6b2aecc4f7d8
parent 5547 f4b087cbb361
child 13728 882756847a04
permissions -rw-r--r--
6939203: JSR 292 needs method handle constants Summary: Add new CP types CONSTANT_MethodHandle, CONSTANT_MethodType; extend 'ldc' bytecode. Reviewed-by: twisti, never
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5341
diff changeset
     2
 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. 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
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5341
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5341
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5341
diff changeset
    21
 * questions.
1
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.interpreter;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import java.util.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.Constructor;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
import sun.jvm.hotspot.oops.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
import sun.jvm.hotspot.utilities.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
public class BytecodeDisassembler {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
   private Method method;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
   private static Map bytecode2Class = new HashMap(); // Map<int, Class>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
   private static void addBytecodeClass(int bytecode, Class clazz) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
      bytecode2Class.put(new Integer(bytecode), clazz);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
   private static Class getBytecodeClass(int bytecode) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
      return (Class) bytecode2Class.get(new Integer(bytecode));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
   static {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
      addBytecodeClass(Bytecodes._anewarray, BytecodeANewArray.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
      addBytecodeClass(Bytecodes._bipush, BytecodeBipush.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
      addBytecodeClass(Bytecodes._checkcast, BytecodeCheckCast.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
      addBytecodeClass(Bytecodes._getfield, BytecodeGetField.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
      addBytecodeClass(Bytecodes._getstatic, BytecodeGetStatic.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
      addBytecodeClass(Bytecodes._goto, BytecodeGoto.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
      addBytecodeClass(Bytecodes._goto_w, BytecodeGotoW.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
      addBytecodeClass(Bytecodes._ifeq, BytecodeIf.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
      addBytecodeClass(Bytecodes._ifne, BytecodeIf.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
      addBytecodeClass(Bytecodes._iflt, BytecodeIf.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
      addBytecodeClass(Bytecodes._ifge, BytecodeIf.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
      addBytecodeClass(Bytecodes._ifgt, BytecodeIf.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
      addBytecodeClass(Bytecodes._ifle, BytecodeIf.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
      addBytecodeClass(Bytecodes._if_icmpeq, BytecodeIf.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
      addBytecodeClass(Bytecodes._if_icmpne, BytecodeIf.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
      addBytecodeClass(Bytecodes._if_icmplt, BytecodeIf.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
      addBytecodeClass(Bytecodes._if_icmpge, BytecodeIf.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
      addBytecodeClass(Bytecodes._if_icmpgt, BytecodeIf.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
      addBytecodeClass(Bytecodes._if_icmple, BytecodeIf.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
      addBytecodeClass(Bytecodes._if_acmpeq, BytecodeIf.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
      addBytecodeClass(Bytecodes._if_acmpne, BytecodeIf.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
      addBytecodeClass(Bytecodes._ifnull, BytecodeIf.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
      addBytecodeClass(Bytecodes._ifnonnull, BytecodeIf.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
      addBytecodeClass(Bytecodes._iinc, BytecodeIinc.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
      addBytecodeClass(Bytecodes._instanceof, BytecodeInstanceOf.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
      addBytecodeClass(Bytecodes._invokevirtual, BytecodeInvoke.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
      addBytecodeClass(Bytecodes._invokestatic, BytecodeInvoke.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
      addBytecodeClass(Bytecodes._invokespecial, BytecodeInvoke.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
      addBytecodeClass(Bytecodes._invokeinterface, BytecodeInvoke.class);
5882
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
    75
      addBytecodeClass(Bytecodes._invokedynamic, BytecodeInvoke.class);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
      addBytecodeClass(Bytecodes._jsr, BytecodeJsr.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
      addBytecodeClass(Bytecodes._jsr_w, BytecodeJsrW.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
      addBytecodeClass(Bytecodes._iload, BytecodeLoad.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
      addBytecodeClass(Bytecodes._lload, BytecodeLoad.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
      addBytecodeClass(Bytecodes._fload, BytecodeLoad.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
      addBytecodeClass(Bytecodes._dload, BytecodeLoad.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
      addBytecodeClass(Bytecodes._aload, BytecodeLoad.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
      addBytecodeClass(Bytecodes._ldc,   BytecodeLoadConstant.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
      addBytecodeClass(Bytecodes._ldc_w, BytecodeLoadConstant.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
      addBytecodeClass(Bytecodes._ldc2_w, BytecodeLoadConstant.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
      addBytecodeClass(Bytecodes._lookupswitch, BytecodeLookupswitch.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
      addBytecodeClass(Bytecodes._multianewarray, BytecodeMultiANewArray.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
      addBytecodeClass(Bytecodes._new, BytecodeNew.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
      addBytecodeClass(Bytecodes._newarray, BytecodeNewArray.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
      addBytecodeClass(Bytecodes._putfield, BytecodePutField.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
      addBytecodeClass(Bytecodes._putstatic, BytecodePutStatic.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
      addBytecodeClass(Bytecodes._ret, BytecodeRet.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
      addBytecodeClass(Bytecodes._sipush, BytecodeSipush.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
      addBytecodeClass(Bytecodes._istore, BytecodeStore.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
      addBytecodeClass(Bytecodes._lstore, BytecodeStore.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
      addBytecodeClass(Bytecodes._fstore, BytecodeStore.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
      addBytecodeClass(Bytecodes._dstore, BytecodeStore.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
      addBytecodeClass(Bytecodes._astore, BytecodeStore.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
      addBytecodeClass(Bytecodes._tableswitch, BytecodeTableswitch.class);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
   public BytecodeDisassembler(Method method) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
      this.method = method;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
   public Method getMethod() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
      return method;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
   public void decode(BytecodeVisitor visitor) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
      visitor.prologue(method);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
      BytecodeStream stream = new BytecodeStream(method);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
      int javacode = Bytecodes._illegal;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
      while ( (javacode = stream.next()) != Bytecodes._illegal) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
         // look for special Bytecode class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
         int bci = stream.bci();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
         int hotspotcode = method.getBytecodeOrBPAt(bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
         Class clazz = getBytecodeClass(javacode);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
         if (clazz == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
            // check for fast_(i|a)_access_0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
            clazz = getBytecodeClass(hotspotcode);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
            if (clazz == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
               // use generic bytecode class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
               clazz = Bytecode.class;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
         // All bytecode classes must have a constructor with signature
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
         // (Lsun/jvm/hotspot/oops/Method;I)V
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
         Constructor cstr = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
         try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
            cstr = clazz.getDeclaredConstructor(new Class[] { Method.class, Integer.TYPE });
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
         } catch(NoSuchMethodException nomethod) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
            if (Assert.ASSERTS_ENABLED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
               Assert.that(false, "Bytecode class without proper constructor!");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
         Object bytecodeObj = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
         try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
            bytecodeObj = cstr.newInstance(new Object[] { method, new Integer(bci) });
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
         } catch (Exception exp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
            if (Assert.ASSERTS_ENABLED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
               Assert.that(false, "Bytecode instance of class "
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
                           + clazz.getName() + " can not be created!");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
         if (stream.isWide()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
            visitor.visit(new Bytecode(method, bci - 1));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
         try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
            visitor.visit((Bytecode) bytecodeObj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
         } catch(ClassCastException castfail) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
            if (Assert.ASSERTS_ENABLED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
               Assert.that(false, clazz.getName() + " is not derived from Bytecode!");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
      visitor.epilogue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
}