src/share/vm/interpreter/rewriter.hpp

Mon, 07 Jul 2014 10:12:40 +0200

author
stefank
date
Mon, 07 Jul 2014 10:12:40 +0200
changeset 6992
2c6ef90f030a
parent 6307
10c9507f544a
child 6876
710a3c8b516e
permissions
-rw-r--r--

8049421: G1 Class Unloading after completing a concurrent mark cycle
Reviewed-by: tschatzl, ehelin, brutisso, coleenp, roland, iveresov
Contributed-by: stefan.karlsson@oracle.com, mikael.gerdin@oracle.com

duke@435 1 /*
coleenp@6307 2 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #ifndef SHARE_VM_INTERPRETER_REWRITER_HPP
stefank@2314 26 #define SHARE_VM_INTERPRETER_REWRITER_HPP
stefank@2314 27
stefank@2314 28 #include "memory/allocation.hpp"
stefank@2314 29 #include "runtime/handles.inline.hpp"
stefank@2314 30 #include "utilities/growableArray.hpp"
stefank@2314 31
duke@435 32 // The Rewriter adds caches to the constant pool and rewrites bytecode indices
duke@435 33 // pointing into the constant pool for better interpreter performance.
duke@435 34
jrose@1161 35 class Rewriter: public StackObj {
duke@435 36 private:
jrose@1161 37 instanceKlassHandle _klass;
jrose@1161 38 constantPoolHandle _pool;
coleenp@4037 39 Array<Method*>* _methods;
jrose@1161 40 intArray _cp_map;
coleenp@4037 41 intStack _cp_cache_map; // for Methodref, Fieldref,
coleenp@4037 42 // InterfaceMethodref and InvokeDynamic
coleenp@4037 43 intArray _reference_map; // maps from cp index to resolved_refs index (or -1)
coleenp@4037 44 intStack _resolved_references_map; // for strings, methodHandle, methodType
coleenp@4037 45 intStack _invokedynamic_references_map; // for invokedynamic resolved refs
twisti@3969 46 intArray _method_handle_invokers;
coleenp@4037 47 int _resolved_reference_limit;
jrose@1161 48
coleenp@6081 49 // For mapping invokedynamic bytecodes, which are discovered during method
coleenp@6081 50 // scanning. The invokedynamic entries are added at the end of the cpCache.
coleenp@6081 51 // If there are any invokespecial/InterfaceMethodref special case bytecodes,
coleenp@6081 52 // these entries are added before invokedynamic entries so that the
coleenp@6081 53 // invokespecial bytecode 16 bit index doesn't overflow.
coleenp@6081 54 intStack _invokedynamic_cp_cache_map;
coleenp@6081 55
coleenp@6081 56 // For patching.
coleenp@6081 57 GrowableArray<address>* _patch_invokedynamic_bcps;
coleenp@6081 58 GrowableArray<int>* _patch_invokedynamic_refs;
coleenp@6081 59
coleenp@4037 60 void init_maps(int length) {
jrose@1161 61 _cp_map.initialize(length, -1);
jrose@1161 62 // Choose an initial value large enough that we don't get frequent
jrose@1161 63 // calls to grow().
coleenp@6081 64 _cp_cache_map.initialize(length/2);
coleenp@4037 65 // Also cache resolved objects, in another different cache.
coleenp@4037 66 _reference_map.initialize(length, -1);
coleenp@6081 67 _resolved_references_map.initialize(length/2);
coleenp@6081 68 _invokedynamic_references_map.initialize(length/2);
coleenp@4037 69 _resolved_reference_limit = -1;
coleenp@6081 70 _first_iteration_cp_cache_limit = -1;
coleenp@6081 71
coleenp@6081 72 // invokedynamic specific fields
coleenp@6081 73 _invokedynamic_cp_cache_map.initialize(length/4);
coleenp@6081 74 _patch_invokedynamic_bcps = new GrowableArray<address>(length/4);
coleenp@6081 75 _patch_invokedynamic_refs = new GrowableArray<int>(length/4);
jrose@1161 76 }
coleenp@4037 77
coleenp@6081 78 int _first_iteration_cp_cache_limit;
coleenp@4037 79 void record_map_limits() {
coleenp@6081 80 // Record initial size of the two arrays generated for the CP cache
coleenp@6081 81 // relative to walking the constant pool.
coleenp@6081 82 _first_iteration_cp_cache_limit = _cp_cache_map.length();
coleenp@4037 83 _resolved_reference_limit = _resolved_references_map.length();
coleenp@4037 84 }
coleenp@4037 85
coleenp@6081 86 int cp_cache_delta() {
coleenp@6081 87 // How many cp cache entries were added since recording map limits after
coleenp@6081 88 // cp cache initialization?
coleenp@6081 89 assert(_first_iteration_cp_cache_limit != -1, "only valid after first iteration");
coleenp@6081 90 return _cp_cache_map.length() - _first_iteration_cp_cache_limit;
coleenp@6081 91 }
coleenp@6081 92
jrose@1161 93 int cp_entry_to_cp_cache(int i) { assert(has_cp_cache(i), "oob"); return _cp_map[i]; }
jrose@1161 94 bool has_cp_cache(int i) { return (uint)i < (uint)_cp_map.length() && _cp_map[i] >= 0; }
coleenp@4037 95
coleenp@6081 96 int add_map_entry(int cp_index, intArray* cp_map, intStack* cp_cache_map) {
coleenp@6081 97 assert(cp_map->at(cp_index) == -1, "not twice on same cp_index");
coleenp@6081 98 int cache_index = cp_cache_map->append(cp_index);
coleenp@6081 99 cp_map->at_put(cp_index, cache_index);
coleenp@6081 100 return cache_index;
coleenp@6081 101 }
coleenp@6081 102
jrose@1161 103 int add_cp_cache_entry(int cp_index) {
coleenp@4037 104 assert(_pool->tag_at(cp_index).value() != JVM_CONSTANT_InvokeDynamic, "use indy version");
coleenp@6081 105 assert(_first_iteration_cp_cache_limit == -1, "do not add cache entries after first iteration");
coleenp@6081 106 int cache_index = add_map_entry(cp_index, &_cp_map, &_cp_cache_map);
jrose@1161 107 assert(cp_entry_to_cp_cache(cp_index) == cache_index, "");
coleenp@4037 108 assert(cp_cache_entry_pool_index(cache_index) == cp_index, "");
jrose@1161 109 return cache_index;
jrose@1161 110 }
coleenp@4037 111
coleenp@4037 112 int add_invokedynamic_cp_cache_entry(int cp_index) {
coleenp@4037 113 assert(_pool->tag_at(cp_index).value() == JVM_CONSTANT_InvokeDynamic, "use non-indy version");
coleenp@6081 114 assert(_first_iteration_cp_cache_limit >= 0, "add indy cache entries after first iteration");
coleenp@6081 115 // add to the invokedynamic index map.
coleenp@6081 116 int cache_index = _invokedynamic_cp_cache_map.append(cp_index);
coleenp@6081 117 // do not update _cp_map, since the mapping is one-to-many
coleenp@6081 118 assert(invokedynamic_cp_cache_entry_pool_index(cache_index) == cp_index, "");
coleenp@6081 119 // this index starts at one but in the bytecode it's appended to the end.
coleenp@6081 120 return cache_index + _first_iteration_cp_cache_limit;
coleenp@6081 121 }
coleenp@6081 122
coleenp@6081 123 int invokedynamic_cp_cache_entry_pool_index(int cache_index) {
coleenp@6081 124 int cp_index = _invokedynamic_cp_cache_map[cache_index];
coleenp@6081 125 return cp_index;
coleenp@6081 126 }
coleenp@6081 127
coleenp@6081 128 // add a new CP cache entry beyond the normal cache for the special case of
coleenp@6081 129 // invokespecial with InterfaceMethodref as cpool operand.
coleenp@6081 130 int add_invokespecial_cp_cache_entry(int cp_index) {
coleenp@6081 131 assert(_first_iteration_cp_cache_limit >= 0, "add these special cache entries after first iteration");
coleenp@6081 132 // Don't add InterfaceMethodref if it already exists at the end.
coleenp@6081 133 for (int i = _first_iteration_cp_cache_limit; i < _cp_cache_map.length(); i++) {
coleenp@6081 134 if (cp_cache_entry_pool_index(i) == cp_index) {
coleenp@6081 135 return i;
coleenp@6081 136 }
coleenp@6081 137 }
coleenp@4037 138 int cache_index = _cp_cache_map.append(cp_index);
coleenp@6081 139 assert(cache_index >= _first_iteration_cp_cache_limit, "");
coleenp@4037 140 // do not update _cp_map, since the mapping is one-to-many
coleenp@4037 141 assert(cp_cache_entry_pool_index(cache_index) == cp_index, "");
jrose@1494 142 return cache_index;
jrose@1494 143 }
jrose@1161 144
coleenp@4037 145 int cp_entry_to_resolved_references(int cp_index) const {
coleenp@4037 146 assert(has_entry_in_resolved_references(cp_index), "oob");
coleenp@4037 147 return _reference_map[cp_index];
coleenp@4037 148 }
coleenp@4037 149 bool has_entry_in_resolved_references(int cp_index) const {
coleenp@4037 150 return (uint)cp_index < (uint)_reference_map.length() && _reference_map[cp_index] >= 0;
coleenp@4037 151 }
coleenp@4037 152
coleenp@4037 153 // add a new entry to the resolved_references map
coleenp@4037 154 int add_resolved_references_entry(int cp_index) {
coleenp@6081 155 int ref_index = add_map_entry(cp_index, &_reference_map, &_resolved_references_map);
coleenp@4037 156 assert(cp_entry_to_resolved_references(cp_index) == ref_index, "");
coleenp@4037 157 return ref_index;
coleenp@4037 158 }
coleenp@4037 159
twisti@4133 160 // add a new entries to the resolved_references map (for invokedynamic and invokehandle only)
twisti@4133 161 int add_invokedynamic_resolved_references_entries(int cp_index, int cache_index) {
coleenp@4037 162 assert(_resolved_reference_limit >= 0, "must add indy refs after first iteration");
twisti@4133 163 int ref_index = -1;
twisti@4133 164 for (int entry = 0; entry < ConstantPoolCacheEntry::_indy_resolved_references_entries; entry++) {
twisti@4133 165 const int index = _resolved_references_map.append(cp_index); // many-to-one
twisti@4133 166 assert(index >= _resolved_reference_limit, "");
twisti@4133 167 if (entry == 0) {
twisti@4133 168 ref_index = index;
twisti@4133 169 }
twisti@4133 170 assert((index - entry) == ref_index, "entries must be consecutive");
twisti@4133 171 _invokedynamic_references_map.at_put_grow(index, cache_index, -1);
twisti@4133 172 }
coleenp@4037 173 return ref_index;
coleenp@4037 174 }
coleenp@4037 175
coleenp@4037 176 int resolved_references_entry_to_pool_index(int ref_index) {
coleenp@4037 177 int cp_index = _resolved_references_map[ref_index];
coleenp@4037 178 return cp_index;
coleenp@4037 179 }
coleenp@4037 180
jrose@2015 181 // Access the contents of _cp_cache_map to determine CP cache layout.
jrose@2015 182 int cp_cache_entry_pool_index(int cache_index) {
jrose@2015 183 int cp_index = _cp_cache_map[cache_index];
coleenp@6081 184 return cp_index;
jrose@2015 185 }
jrose@2015 186
jrose@1161 187 // All the work goes in here:
coleenp@4037 188 Rewriter(instanceKlassHandle klass, constantPoolHandle cpool, Array<Method*>* methods, TRAPS);
jrose@1161 189
jrose@1161 190 void compute_index_maps();
jrose@1161 191 void make_constant_pool_cache(TRAPS);
coleenp@6121 192 void scan_method(Method* m, bool reverse, bool* invokespecial_error);
jrose@1161 193 void rewrite_Object_init(methodHandle m, TRAPS);
coleenp@6081 194 void rewrite_member_reference(address bcp, int offset, bool reverse);
coleenp@6081 195 void maybe_rewrite_invokehandle(address opc, int cp_index, int cache_index, bool reverse);
coleenp@6081 196 void rewrite_invokedynamic(address bcp, int offset, bool reverse);
coleenp@6081 197 void maybe_rewrite_ldc(address bcp, int offset, bool is_wide, bool reverse);
coleenp@6121 198 void rewrite_invokespecial(address bcp, int offset, bool reverse, bool* invokespecial_error);
coleenp@6081 199
coleenp@6081 200 void patch_invokedynamic_bytecodes();
coleenp@6081 201
coleenp@6307 202 // Do all the work.
coleenp@6307 203 void rewrite_bytecodes(TRAPS);
coleenp@6307 204
coleenp@2945 205 // Revert bytecodes in case of an exception.
coleenp@6121 206 void restore_bytecodes();
duke@435 207
coleenp@2945 208 static methodHandle rewrite_jsrs(methodHandle m, TRAPS);
duke@435 209 public:
jrose@1161 210 // Driver routine:
duke@435 211 static void rewrite(instanceKlassHandle klass, TRAPS);
duke@435 212 };
stefank@2314 213
stefank@2314 214 #endif // SHARE_VM_INTERPRETER_REWRITER_HPP

mercurial