bytecodeStream.hpp revision 1472:c18cbe5936b8
1/*
2 * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25// A BytecodeStream is used for fast iteration over the bytecodes
26// of a methodOop.
27//
28// Usage:
29//
30// BytecodeStream s(method);
31// Bytecodes::Code c;
32// while ((c = s.next()) >= 0) {
33//   ...
34// }
35//
36// A RawBytecodeStream is a simple version of BytecodeStream.
37// It is used ONLY when we know the bytecodes haven't been rewritten
38// yet, such as in the rewriter or the verifier. Currently only the
39// verifier uses this class.
40
41class RawBytecodeStream: StackObj {
42 protected:
43  // stream buffer
44  methodHandle    _method;                       // read from method directly
45
46  // reading position
47  int             _bci;                          // bci if current bytecode
48  int             _next_bci;                     // bci of next bytecode
49  int             _end_bci;                      // bci after the current iteration interval
50
51  // last bytecode read
52  Bytecodes::Code _code;
53  bool            _is_wide;
54
55 public:
56  // Construction
57  RawBytecodeStream(methodHandle method) : _method(method) {
58    set_interval(0, _method->code_size());
59  }
60
61  // Iteration control
62  void set_interval(int beg_bci, int end_bci) {
63    // iterate over the interval [beg_bci, end_bci)
64    assert(0 <= beg_bci && beg_bci <= method()->code_size(), "illegal beg_bci");
65    assert(0 <= end_bci && end_bci <= method()->code_size(), "illegal end_bci");
66    // setup of iteration pointers
67    _bci      = beg_bci;
68    _next_bci = beg_bci;
69    _end_bci  = end_bci;
70  }
71  void set_start   (int beg_bci) {
72    set_interval(beg_bci, _method->code_size());
73  }
74
75  // Iteration
76  // Use raw_next() rather than next() for faster method reference
77  Bytecodes::Code raw_next() {
78    Bytecodes::Code code;
79    // set reading position
80    _bci = _next_bci;
81    assert(!is_last_bytecode(), "caller should check is_last_bytecode()");
82
83    address bcp = RawBytecodeStream::bcp();
84    code        = Bytecodes::code_or_bp_at(bcp);
85
86    // set next bytecode position
87    int l = Bytecodes::length_for(code);
88    if (l > 0 && (_bci + l) <= _end_bci) {
89      assert(code != Bytecodes::_wide && code != Bytecodes::_tableswitch
90             && code != Bytecodes::_lookupswitch, "can't be special bytecode");
91      _is_wide = false;
92      _next_bci += l;
93      _code = code;
94      return code;
95    } else if (code == Bytecodes::_wide && _bci + 1 >= _end_bci) {
96      return Bytecodes::_illegal;
97    } else {
98      return raw_next_special(code);
99    }
100  }
101  Bytecodes::Code raw_next_special(Bytecodes::Code code);
102
103  // Stream attributes
104  methodHandle    method() const                 { return _method; }
105
106  int             bci() const                    { return _bci; }
107  int             next_bci() const               { return _next_bci; }
108  int             end_bci() const                { return _end_bci; }
109
110  Bytecodes::Code code() const                   { return _code; }
111  bool            is_wide() const                { return _is_wide; }
112  int             instruction_size() const       { return (_next_bci - _bci); }
113  bool            is_last_bytecode() const       { return _next_bci >= _end_bci; }
114
115  address         bcp() const                    { return method()->code_base() + _bci; }
116  address         next_bcp()                     { return method()->code_base() + _next_bci; }
117
118  // State changes
119  void            set_next_bci(int bci)          { assert(0 <= bci && bci <= method()->code_size(), "illegal bci"); _next_bci = bci; }
120
121  // Bytecode-specific attributes
122  int             dest() const                   { return bci() + (short)Bytes::get_Java_u2(bcp() + 1); }
123  int             dest_w() const                 { return bci() + (int  )Bytes::get_Java_u4(bcp() + 1); }
124
125  // Unsigned indices, widening
126  int             get_index() const              { assert_index_size(is_wide() ? 2 : 1);
127                                                   return (is_wide()) ? Bytes::get_Java_u2(bcp() + 2) : bcp()[1]; }
128  int             get_index_big() const          { assert_index_size(2);
129                                                   return (int)Bytes::get_Java_u2(bcp() + 1);  }
130  int             get_index_int() const          { return has_giant_index() ? get_index_giant() : get_index_big(); }
131  int             get_index_giant() const        { assert_index_size(4); return Bytes::get_native_u4(bcp() + 1); }
132  int             has_giant_index() const        { return (code() == Bytecodes::_invokedynamic); }
133
134 private:
135  void assert_index_size(int required_size) const {
136#ifdef ASSERT
137    int isize = instruction_size() - (int)_is_wide - 1;
138    if (isize == 2 && code() == Bytecodes::_iinc)
139      isize = 1;
140    else if (isize <= 2)
141      ;                         // no change
142    else if (has_giant_index())
143      isize = 4;
144    else
145      isize = 2;
146    assert(isize = required_size, "wrong index size");
147#endif
148  }
149};
150
151// In BytecodeStream, non-java bytecodes will be translated into the
152// corresponding java bytecodes.
153
154class BytecodeStream: public RawBytecodeStream {
155 public:
156  // Construction
157  BytecodeStream(methodHandle method) : RawBytecodeStream(method) { }
158
159  // Iteration
160  Bytecodes::Code next() {
161    Bytecodes::Code code;
162    // set reading position
163    _bci = _next_bci;
164    if (is_last_bytecode()) {
165      // indicate end of bytecode stream
166      code = Bytecodes::_illegal;
167    } else {
168      // get bytecode
169      address bcp = BytecodeStream::bcp();
170      code        = Bytecodes::java_code_at(bcp);
171      // set next bytecode position
172      //
173      // note that we cannot advance before having the
174      // tty bytecode otherwise the stepping is wrong!
175      // (carefull: length_for(...) must be used first!)
176      int l = Bytecodes::length_for(code);
177      if (l == 0) l = Bytecodes::length_at(bcp);
178      _next_bci  += l;
179      assert(_bci < _next_bci, "length must be > 0");
180      // set attributes
181      _is_wide      = false;
182      // check for special (uncommon) cases
183      if (code == Bytecodes::_wide) {
184        code = (Bytecodes::Code)bcp[1];
185        _is_wide = true;
186      }
187      assert(Bytecodes::is_java_code(code), "sanity check");
188    }
189    _code = code;
190    return _code;
191  }
192
193  bool            is_active_breakpoint() const   { return Bytecodes::is_active_breakpoint_at(bcp()); }
194};
195