hotspot/src/share/vm/interpreter/bytecodeStream.hpp
author trims
Thu, 27 May 2010 19:08:38 -0700
changeset 5547 f4b087cbb361
parent 3261 c7d5aae8d3f7
child 5702 201c5cde25bb
permissions -rw-r--r--
6941466: Oracle rebranding changes for Hotspot repositories Summary: Change all the Sun copyrights to Oracle copyright Reviewed-by: ohair
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: 3261
diff changeset
     2
 * Copyright (c) 1997, 2009, 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: 3261
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3261
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: 3261
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
// A BytecodeStream is used for fast iteration over the bytecodes
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
// of a methodOop.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
// Usage:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
// BytecodeStream s(method);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
// Bytecodes::Code c;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
// while ((c = s.next()) >= 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
//   ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
// }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
// A RawBytecodeStream is a simple version of BytecodeStream.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
// It is used ONLY when we know the bytecodes haven't been rewritten
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
// yet, such as in the rewriter or the verifier. Currently only the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
// verifier uses this class.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
class RawBytecodeStream: StackObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  // stream buffer
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  methodHandle    _method;                       // read from method directly
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  // reading position
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  int             _bci;                          // bci if current bytecode
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  int             _next_bci;                     // bci of next bytecode
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  int             _end_bci;                      // bci after the current iteration interval
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  // last bytecode read
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  Bytecodes::Code _code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  bool            _is_wide;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  // Construction
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  RawBytecodeStream(methodHandle method) : _method(method) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
    set_interval(0, _method->code_size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  // Iteration control
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  void set_interval(int beg_bci, int end_bci) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
    // iterate over the interval [beg_bci, end_bci)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
    assert(0 <= beg_bci && beg_bci <= method()->code_size(), "illegal beg_bci");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
    assert(0 <= end_bci && end_bci <= method()->code_size(), "illegal end_bci");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
    // setup of iteration pointers
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
    _bci      = beg_bci;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
    _next_bci = beg_bci;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
    _end_bci  = end_bci;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  void set_start   (int beg_bci) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
    set_interval(beg_bci, _method->code_size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  // Iteration
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  // Use raw_next() rather than next() for faster method reference
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  Bytecodes::Code raw_next() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
    Bytecodes::Code code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
    // set reading position
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
    _bci = _next_bci;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
    assert(!is_last_bytecode(), "caller should check is_last_bytecode()");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
    address bcp = RawBytecodeStream::bcp();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
    code        = Bytecodes::code_or_bp_at(bcp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
    // set next bytecode position
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
    int l = Bytecodes::length_for(code);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
    if (l > 0 && (_bci + l) <= _end_bci) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
      assert(code != Bytecodes::_wide && code != Bytecodes::_tableswitch
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
             && code != Bytecodes::_lookupswitch, "can't be special bytecode");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
      _is_wide = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
      _next_bci += l;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
      _code = code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
      return code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
    } else if (code == Bytecodes::_wide && _bci + 1 >= _end_bci) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
      return Bytecodes::_illegal;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
      return raw_next_special(code);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  Bytecodes::Code raw_next_special(Bytecodes::Code code);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  // Stream attributes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  methodHandle    method() const                 { return _method; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  int             bci() const                    { return _bci; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  int             next_bci() const               { return _next_bci; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  int             end_bci() const                { return _end_bci; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  Bytecodes::Code code() const                   { return _code; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  bool            is_wide() const                { return _is_wide; }
2570
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   112
  int             instruction_size() const       { return (_next_bci - _bci); }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  bool            is_last_bytecode() const       { return _next_bci >= _end_bci; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  address         bcp() const                    { return method()->code_base() + _bci; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  address         next_bcp()                     { return method()->code_base() + _next_bci; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  // State changes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  void            set_next_bci(int bci)          { assert(0 <= bci && bci <= method()->code_size(), "illegal bci"); _next_bci = bci; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  // Bytecode-specific attributes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  int             dest() const                   { return bci() + (short)Bytes::get_Java_u2(bcp() + 1); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  int             dest_w() const                 { return bci() + (int  )Bytes::get_Java_u4(bcp() + 1); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  // Unsigned indices, widening
2570
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   126
  int             get_index() const              { assert_index_size(is_wide() ? 2 : 1);
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   127
                                                   return (is_wide()) ? Bytes::get_Java_u2(bcp() + 2) : bcp()[1]; }
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   128
  int             get_index_big() const          { assert_index_size(2);
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   129
                                                   return (int)Bytes::get_Java_u2(bcp() + 1);  }
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   130
  int             get_index_int() const          { return has_giant_index() ? get_index_giant() : get_index_big(); }
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   131
  int             get_index_giant() const        { assert_index_size(4); return Bytes::get_native_u4(bcp() + 1); }
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   132
  int             has_giant_index() const        { return (code() == Bytecodes::_invokedynamic); }
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   133
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   134
 private:
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   135
  void assert_index_size(int required_size) const {
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   136
#ifdef ASSERT
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   137
    int isize = instruction_size() - (int)_is_wide - 1;
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   138
    if (isize == 2 && code() == Bytecodes::_iinc)
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   139
      isize = 1;
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   140
    else if (isize <= 2)
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   141
      ;                         // no change
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   142
    else if (has_giant_index())
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   143
      isize = 4;
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   144
    else
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   145
      isize = 2;
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   146
    assert(isize = required_size, "wrong index size");
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   147
#endif
ecc7862946d4 6655646: dynamic languages need dynamically linked call sites
jrose
parents: 1
diff changeset
   148
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
// In BytecodeStream, non-java bytecodes will be translated into the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
// corresponding java bytecodes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
class BytecodeStream: public RawBytecodeStream {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  // Construction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  BytecodeStream(methodHandle method) : RawBytecodeStream(method) { }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  // Iteration
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  Bytecodes::Code next() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
    Bytecodes::Code code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
    // set reading position
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
    _bci = _next_bci;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
    if (is_last_bytecode()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
      // indicate end of bytecode stream
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
      code = Bytecodes::_illegal;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
      // get bytecode
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
      address bcp = BytecodeStream::bcp();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
      code        = Bytecodes::java_code_at(bcp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
      // set next bytecode position
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
      //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
      // note that we cannot advance before having the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
      // tty bytecode otherwise the stepping is wrong!
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
      // (carefull: length_for(...) must be used first!)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
      int l = Bytecodes::length_for(code);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
      if (l == 0) l = Bytecodes::length_at(bcp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
      _next_bci  += l;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
      assert(_bci < _next_bci, "length must be > 0");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
      // set attributes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
      _is_wide      = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
      // check for special (uncommon) cases
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
      if (code == Bytecodes::_wide) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
        code = (Bytecodes::Code)bcp[1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
        _is_wide = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
      assert(Bytecodes::is_java_code(code), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
    _code = code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
    return _code;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  bool            is_active_breakpoint() const   { return Bytecodes::is_active_breakpoint_at(bcp()); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
};