hotspot/agent/src/share/classes/sun/jvm/hotspot/interpreter/BytecodeStream.java
author never
Thu, 21 Jul 2011 08:38:25 -0700
changeset 10251 71b8938a2821
parent 5547 f4b087cbb361
permissions -rw-r--r--
7012081: JSR 292: SA-JDI can't read MH/MT/Indy ConstantPool entries Reviewed-by: kvn, twisti, jrose
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
10251
71b8938a2821 7012081: JSR 292: SA-JDI can't read MH/MT/Indy ConstantPool entries
never
parents: 5547
diff changeset
     2
 * Copyright (c) 2001, 2011, 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 BytecodeStream {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  private Method _method;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
  // reading position
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  private int     _bci;       // bci if current bytecode
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  private int     _next_bci;  // bci of next bytecode
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  private int     _end_bci;   // bci after the current iteration interval
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  // last bytecode read
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  private int     _code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  private boolean _is_wide;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  // Construction
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  public BytecodeStream(Method method) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
    _method = method;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
    setInterval(0, (int) method.getCodeSize());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  // Iteration control
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  public void setInterval(int beg_bci, int end_bci) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
    if (Assert.ASSERTS_ENABLED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
      Assert.that(0 <= beg_bci && beg_bci <= _method.getCodeSize(), "illegal beg_bci");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
      Assert.that(0 <= end_bci && end_bci <= _method.getCodeSize(), "illegal end_bci");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
    // setup of iteration pointers
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    _bci      = beg_bci;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
    _next_bci = beg_bci;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
    _end_bci  = end_bci;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  public void setStart(int beg_bci) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
    setInterval(beg_bci, (int) _method.getCodeSize());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  // Iteration
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  public int next() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
    int code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
    // set reading position
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
    _bci = _next_bci;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
    if (isLastBytecode()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
      // indicate end of bytecode stream
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
      code = Bytecodes._illegal;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
      // get bytecode
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
      int rawCode = Bytecodes.codeAt(_method, _bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
      code = 0; // Make javac happy
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
      try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
        code = Bytecodes.javaCode(rawCode);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
      } catch (AssertionFailure e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
        e.printStackTrace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
        Assert.that(false, "Failure occurred at bci " + _bci + " in method " + _method.externalNameAndSignature());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
      // set next bytecode position
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
      //
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
      int l = Bytecodes.lengthFor(code);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
      if (l == 0) l = Bytecodes.lengthAt(_method, _bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
      _next_bci  += l;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
      if (Assert.ASSERTS_ENABLED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
        Assert.that(_bci < _next_bci, "length must be > 0");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
      // set attributes
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
      _is_wide      = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
      // check for special (uncommon) cases
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
      if (code == Bytecodes._wide) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
        code = _method.getBytecodeOrBPAt(_bci + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
        _is_wide = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
      if (Assert.ASSERTS_ENABLED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
        Assert.that(Bytecodes.isJavaCode(code), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
    _code = code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    return _code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  // Stream attributes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  public Method  method()             { return _method; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  public int     bci()                { return _bci; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  public int     nextBCI()            { return _next_bci; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  public int     endBCI()             { return _end_bci; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  public int     code()               { return _code; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  public boolean isWide()             { return _is_wide; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  public boolean isActiveBreakpoint() { return Bytecodes.isActiveBreakpointAt(_method, _bci); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  public boolean isLastBytecode()     { return _next_bci >= _end_bci; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  // State changes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  public void    setNextBCI(int bci)  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
    if (Assert.ASSERTS_ENABLED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
      Assert.that(0 <= bci && bci <= _method.getCodeSize(), "illegal bci");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
    _next_bci = bci;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  // Bytecode-specific attributes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  public int     dest()               { return bci() + _method.getBytecodeShortArg(bci() + 1); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  public int     dest_w()             { return bci() + _method.getBytecodeIntArg(bci()   + 1); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  // Unsigned indices, widening
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  public int     getIndex()           { return (isWide())
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
                                          ? (_method.getBytecodeShortArg(bci() + 2) & 0xFFFF)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
                                          : (_method.getBytecodeOrBPAt(bci() + 1) & 0xFF); }
10251
71b8938a2821 7012081: JSR 292: SA-JDI can't read MH/MT/Indy ConstantPool entries
never
parents: 5547
diff changeset
   133
  public int     getIndexU1()         { return _method.getBytecodeOrBPAt(bci() + 1) & 0xFF; }
71b8938a2821 7012081: JSR 292: SA-JDI can't read MH/MT/Indy ConstantPool entries
never
parents: 5547
diff changeset
   134
  public int     getIndexU2()         { return _method.getBytecodeShortArg(bci() + 1) & 0xFFFF; }
71b8938a2821 7012081: JSR 292: SA-JDI can't read MH/MT/Indy ConstantPool entries
never
parents: 5547
diff changeset
   135
  public int     getIndexU4()         { return _method.getNativeIntArg(bci() + 1); }
71b8938a2821 7012081: JSR 292: SA-JDI can't read MH/MT/Indy ConstantPool entries
never
parents: 5547
diff changeset
   136
  public boolean hasIndexU4()         { return code() == Bytecodes._invokedynamic; }
71b8938a2821 7012081: JSR 292: SA-JDI can't read MH/MT/Indy ConstantPool entries
never
parents: 5547
diff changeset
   137
71b8938a2821 7012081: JSR 292: SA-JDI can't read MH/MT/Indy ConstantPool entries
never
parents: 5547
diff changeset
   138
  public int     getIndexU1Cpcache()         { return _method.getBytecodeOrBPAt(bci() + 1) & 0xFF; }
71b8938a2821 7012081: JSR 292: SA-JDI can't read MH/MT/Indy ConstantPool entries
never
parents: 5547
diff changeset
   139
  public int     getIndexU2Cpcache()         { return _method.getNativeShortArg(bci() + 1) & 0xFFFF; }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  // Fetch at absolute BCI (for manual parsing of certain bytecodes)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  public int     codeAt(int bci) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    return _method.getBytecodeOrBPAt(bci);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
}