src/share/vm/interpreter/rewriter.hpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

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

mercurial