hotspot/agent/src/share/classes/sun/jvm/hotspot/interpreter/BytecodeInvoke.java
author jrose
Wed, 09 Jun 2010 18:50:45 -0700
changeset 5882 6b2aecc4f7d8
parent 5547 f4b087cbb361
child 7662 5f31baaff55b
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: 1
diff changeset
     2
 * Copyright (c) 2001, 2003, 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: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
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: 1
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 sun.jvm.hotspot.oops.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
import sun.jvm.hotspot.runtime.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
import sun.jvm.hotspot.utilities.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
public class BytecodeInvoke extends BytecodeWithCPIndex {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  BytecodeInvoke(Method method, int bci) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
    super(method, bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  public static BytecodeInvoke at(Method method, int bci) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
    BytecodeInvoke b = new BytecodeInvoke(method, bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
    if (Assert.ASSERTS_ENABLED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
      b.verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
    return b;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  /** Like at, but returns null if the BCI is not at an invoke */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  public static BytecodeInvoke atCheck(Method method, int bci) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
    BytecodeInvoke b = new BytecodeInvoke(method, bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
    return (b.isValid() ? b : null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  public static BytecodeInvoke at(BytecodeStream bcs) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
    return new BytecodeInvoke(bcs.method(), bcs.bci());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  // returns the name of the invoked method
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  public Symbol name() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    ConstantPool cp = method().getConstants();
5882
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
    57
    if (isInvokedynamic()) {
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
    58
       int[] nt = cp.getNameAndTypeAt(indexForFieldOrMethod());
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
    59
       return cp.getSymbolAt(nt[0]);
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
    60
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
    return cp.getNameRefAt(index());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  // returns the signature of the invoked method
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  public Symbol signature() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
    ConstantPool cp = method().getConstants();
5882
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
    67
    if (isInvokedynamic()) {
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
    68
       int[] nt = cp.getNameAndTypeAt(indexForFieldOrMethod());
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
    69
       return cp.getSymbolAt(nt[1]);
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
    70
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
    return cp.getSignatureRefAt(index());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
5882
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
    74
  public int getSecondaryIndex() {
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
    75
    if (isInvokedynamic()) {
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
    76
      // change byte-ordering of 4-byte integer
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
    77
      return VM.getVM().getBytes().swapInt(javaSignedWordAt(1));
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
    78
    }
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
    79
    return super.getSecondaryIndex();  // throw an error
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
    80
  }
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
    81
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  public Method getInvokedMethod() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
    return method().getConstants().getMethodRefAt(index());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  // returns the result type (see BasicType.java) of the invoke
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  public int resultType() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
    ResultTypeFinder rts = new ResultTypeFinder(signature());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
    rts.iterate();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
    return rts.type();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  public int adjustedInvokeCode() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
    return javaCode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  // "specified" method   (from constant pool)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  // FIXME: elided for now
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  // public Method staticTarget();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  // Testers
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  public boolean isInvokeinterface() { return adjustedInvokeCode() == Bytecodes._invokeinterface; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  public boolean isInvokevirtual()   { return adjustedInvokeCode() == Bytecodes._invokevirtual;   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  public boolean isInvokestatic()    { return adjustedInvokeCode() == Bytecodes._invokestatic;    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  public boolean isInvokespecial()   { return adjustedInvokeCode() == Bytecodes._invokespecial;   }
5882
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
   106
  public boolean isInvokedynamic()   { return adjustedInvokeCode() == Bytecodes._invokedynamic; }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  public boolean isValid()           { return isInvokeinterface() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
                                              isInvokevirtual()   ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
                                              isInvokestatic()    ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
                                              isInvokespecial(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  public void verify() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
    if (Assert.ASSERTS_ENABLED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
      Assert.that(isValid(), "check invoke");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  public String toString() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
    StringBuffer buf = new StringBuffer();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
    buf.append(getJavaBytecodeName());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
    buf.append(spaces);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
    buf.append('#');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
    buf.append(Integer.toString(indexForFieldOrMethod()));
5882
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
   124
    if (isInvokedynamic()) {
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
   125
       buf.append('(');
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
   126
       buf.append(Integer.toString(getSecondaryIndex()));
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
   127
       buf.append(')');
6b2aecc4f7d8 6939203: JSR 292 needs method handle constants
jrose
parents: 5547
diff changeset
   128
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    buf.append(" [Method ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
    StringBuffer sigBuf = new StringBuffer();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
    new SignatureConverter(signature(), sigBuf).iterateReturntype();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    buf.append(sigBuf.toString().replace('/', '.'));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
    buf.append(spaces);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    buf.append(name().asString());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
    buf.append('(');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
    sigBuf = new StringBuffer();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
    new SignatureConverter(signature(), sigBuf).iterateParameters();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
    buf.append(sigBuf.toString().replace('/', '.'));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
    buf.append(')');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
    buf.append(']');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
    if (code() != javaCode()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
       buf.append(spaces);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
       buf.append('[');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
       buf.append(getBytecodeName());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
       buf.append(']');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
    return buf.toString();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
}