rewriter.hpp 9.1 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31
#ifndef SHARE_VM_INTERPRETER_REWRITER_HPP
#define SHARE_VM_INTERPRETER_REWRITER_HPP

#include "memory/allocation.hpp"
#include "runtime/handles.inline.hpp"
#include "utilities/growableArray.hpp"

D
duke 已提交
32 33 34
// The Rewriter adds caches to the constant pool and rewrites bytecode indices
// pointing into the constant pool for better interpreter performance.

35
class Rewriter: public StackObj {
D
duke 已提交
36
 private:
37 38
  instanceKlassHandle _klass;
  constantPoolHandle  _pool;
39
  Array<Method*>*     _methods;
40
  intArray            _cp_map;
41 42 43 44 45
  intStack            _cp_cache_map;        // for Methodref, Fieldref,
                                            // InterfaceMethodref and InvokeDynamic
  intArray            _reference_map;       // maps from cp index to resolved_refs index (or -1)
  intStack            _resolved_references_map;    // for strings, methodHandle, methodType
  intStack            _invokedynamic_references_map; // for invokedynamic resolved refs
46
  intArray            _method_handle_invokers;
47
  int                 _resolved_reference_limit;
48

49 50 51 52 53 54 55 56 57 58 59
  // For mapping invokedynamic bytecodes, which are discovered during method
  // scanning.  The invokedynamic entries are added at the end of the cpCache.
  // If there are any invokespecial/InterfaceMethodref special case bytecodes,
  // these entries are added before invokedynamic entries so that the
  // invokespecial bytecode 16 bit index doesn't overflow.
  intStack            _invokedynamic_cp_cache_map;

  // For patching.
  GrowableArray<address>* _patch_invokedynamic_bcps;
  GrowableArray<int>*     _patch_invokedynamic_refs;

60
  void init_maps(int length) {
61 62 63
    _cp_map.initialize(length, -1);
    // Choose an initial value large enough that we don't get frequent
    // calls to grow().
64
    _cp_cache_map.initialize(length/2);
65 66
    // Also cache resolved objects, in another different cache.
    _reference_map.initialize(length, -1);
67 68
    _resolved_references_map.initialize(length/2);
    _invokedynamic_references_map.initialize(length/2);
69
    _resolved_reference_limit = -1;
70 71 72 73 74 75
    _first_iteration_cp_cache_limit = -1;

    // invokedynamic specific fields
    _invokedynamic_cp_cache_map.initialize(length/4);
    _patch_invokedynamic_bcps = new GrowableArray<address>(length/4);
    _patch_invokedynamic_refs = new GrowableArray<int>(length/4);
76
  }
77

78
  int _first_iteration_cp_cache_limit;
79
  void record_map_limits() {
80 81 82
    // Record initial size of the two arrays generated for the CP cache
    // relative to walking the constant pool.
    _first_iteration_cp_cache_limit = _cp_cache_map.length();
83 84 85
    _resolved_reference_limit = _resolved_references_map.length();
  }

86 87 88 89 90 91 92
  int cp_cache_delta() {
    // How many cp cache entries were added since recording map limits after
    // cp cache initialization?
    assert(_first_iteration_cp_cache_limit != -1, "only valid after first iteration");
    return _cp_cache_map.length() - _first_iteration_cp_cache_limit;
  }

93 94
  int  cp_entry_to_cp_cache(int i) { assert(has_cp_cache(i), "oob"); return _cp_map[i]; }
  bool has_cp_cache(int i) { return (uint)i < (uint)_cp_map.length() && _cp_map[i] >= 0; }
95

96 97 98 99 100 101 102
  int add_map_entry(int cp_index, intArray* cp_map, intStack* cp_cache_map) {
    assert(cp_map->at(cp_index) == -1, "not twice on same cp_index");
    int cache_index = cp_cache_map->append(cp_index);
    cp_map->at_put(cp_index, cache_index);
    return cache_index;
  }

103
  int add_cp_cache_entry(int cp_index) {
104
    assert(_pool->tag_at(cp_index).value() != JVM_CONSTANT_InvokeDynamic, "use indy version");
105 106
    assert(_first_iteration_cp_cache_limit == -1, "do not add cache entries after first iteration");
    int cache_index = add_map_entry(cp_index, &_cp_map, &_cp_cache_map);
107
    assert(cp_entry_to_cp_cache(cp_index) == cache_index, "");
108
    assert(cp_cache_entry_pool_index(cache_index) == cp_index, "");
109 110
    return cache_index;
  }
111 112 113

  int add_invokedynamic_cp_cache_entry(int cp_index) {
    assert(_pool->tag_at(cp_index).value() == JVM_CONSTANT_InvokeDynamic, "use non-indy version");
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    assert(_first_iteration_cp_cache_limit >= 0, "add indy cache entries after first iteration");
    // add to the invokedynamic index map.
    int cache_index = _invokedynamic_cp_cache_map.append(cp_index);
    // do not update _cp_map, since the mapping is one-to-many
    assert(invokedynamic_cp_cache_entry_pool_index(cache_index) == cp_index, "");
    // this index starts at one but in the bytecode it's appended to the end.
    return cache_index + _first_iteration_cp_cache_limit;
  }

  int invokedynamic_cp_cache_entry_pool_index(int cache_index) {
    int cp_index = _invokedynamic_cp_cache_map[cache_index];
    return cp_index;
  }

  // add a new CP cache entry beyond the normal cache for the special case of
  // invokespecial with InterfaceMethodref as cpool operand.
  int add_invokespecial_cp_cache_entry(int cp_index) {
    assert(_first_iteration_cp_cache_limit >= 0, "add these special cache entries after first iteration");
    // Don't add InterfaceMethodref if it already exists at the end.
    for (int i = _first_iteration_cp_cache_limit; i < _cp_cache_map.length(); i++) {
     if (cp_cache_entry_pool_index(i) == cp_index) {
       return i;
     }
    }
138
    int cache_index = _cp_cache_map.append(cp_index);
139
    assert(cache_index >= _first_iteration_cp_cache_limit, "");
140 141
    // do not update _cp_map, since the mapping is one-to-many
    assert(cp_cache_entry_pool_index(cache_index) == cp_index, "");
142 143
    return cache_index;
  }
144

145 146 147 148 149 150 151 152 153 154
  int  cp_entry_to_resolved_references(int cp_index) const {
    assert(has_entry_in_resolved_references(cp_index), "oob");
    return _reference_map[cp_index];
  }
  bool has_entry_in_resolved_references(int cp_index) const {
    return (uint)cp_index < (uint)_reference_map.length() && _reference_map[cp_index] >= 0;
  }

  // add a new entry to the resolved_references map
  int add_resolved_references_entry(int cp_index) {
155
    int ref_index = add_map_entry(cp_index, &_reference_map, &_resolved_references_map);
156 157 158 159
    assert(cp_entry_to_resolved_references(cp_index) == ref_index, "");
    return ref_index;
  }

160 161
  // add a new entries to the resolved_references map (for invokedynamic and invokehandle only)
  int add_invokedynamic_resolved_references_entries(int cp_index, int cache_index) {
162
    assert(_resolved_reference_limit >= 0, "must add indy refs after first iteration");
163 164 165 166 167 168 169 170 171 172
    int ref_index = -1;
    for (int entry = 0; entry < ConstantPoolCacheEntry::_indy_resolved_references_entries; entry++) {
      const int index = _resolved_references_map.append(cp_index);  // many-to-one
      assert(index >= _resolved_reference_limit, "");
      if (entry == 0) {
        ref_index = index;
      }
      assert((index - entry) == ref_index, "entries must be consecutive");
      _invokedynamic_references_map.at_put_grow(index, cache_index, -1);
    }
173 174 175 176 177 178 179 180
    return ref_index;
  }

  int resolved_references_entry_to_pool_index(int ref_index) {
    int cp_index = _resolved_references_map[ref_index];
    return cp_index;
  }

181 182 183
  // Access the contents of _cp_cache_map to determine CP cache layout.
  int cp_cache_entry_pool_index(int cache_index) {
    int cp_index = _cp_cache_map[cache_index];
184
    return cp_index;
185 186
  }

187
  // All the work goes in here:
188
  Rewriter(instanceKlassHandle klass, constantPoolHandle cpool, Array<Method*>* methods, TRAPS);
189 190 191

  void compute_index_maps();
  void make_constant_pool_cache(TRAPS);
192
  void scan_method(Method* m, bool reverse, TRAPS);
193
  void rewrite_Object_init(methodHandle m, TRAPS);
194 195 196 197 198 199 200 201
  void rewrite_member_reference(address bcp, int offset, bool reverse);
  void maybe_rewrite_invokehandle(address opc, int cp_index, int cache_index, bool reverse);
  void rewrite_invokedynamic(address bcp, int offset, bool reverse);
  void maybe_rewrite_ldc(address bcp, int offset, bool is_wide, bool reverse);
  void rewrite_invokespecial(address bcp, int offset, bool reverse, TRAPS);

  void patch_invokedynamic_bytecodes();

202
  // Revert bytecodes in case of an exception.
203
  void restore_bytecodes(TRAPS);
D
duke 已提交
204

205
  static methodHandle rewrite_jsrs(methodHandle m, TRAPS);
D
duke 已提交
206
 public:
207
  // Driver routine:
D
duke 已提交
208 209
  static void rewrite(instanceKlassHandle klass, TRAPS);
};
210 211

#endif // SHARE_VM_INTERPRETER_REWRITER_HPP