duke@435: /* coleenp@4037: * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_INTERPRETER_REWRITER_HPP stefank@2314: #define SHARE_VM_INTERPRETER_REWRITER_HPP stefank@2314: stefank@2314: #include "memory/allocation.hpp" stefank@2314: #include "runtime/handles.inline.hpp" stefank@2314: #include "utilities/growableArray.hpp" stefank@2314: duke@435: // The Rewriter adds caches to the constant pool and rewrites bytecode indices duke@435: // pointing into the constant pool for better interpreter performance. duke@435: jrose@1161: class Rewriter: public StackObj { duke@435: private: jrose@1161: instanceKlassHandle _klass; jrose@1161: constantPoolHandle _pool; coleenp@4037: Array* _methods; jrose@1161: intArray _cp_map; coleenp@4037: intStack _cp_cache_map; // for Methodref, Fieldref, coleenp@4037: // InterfaceMethodref and InvokeDynamic coleenp@4037: intArray _reference_map; // maps from cp index to resolved_refs index (or -1) coleenp@4037: intStack _resolved_references_map; // for strings, methodHandle, methodType coleenp@4037: intStack _invokedynamic_references_map; // for invokedynamic resolved refs twisti@3969: intArray _method_handle_invokers; coleenp@4037: int _resolved_reference_limit; jrose@1161: coleenp@4037: void init_maps(int length) { jrose@1161: _cp_map.initialize(length, -1); jrose@1161: // Choose an initial value large enough that we don't get frequent jrose@1161: // calls to grow(). jrose@1161: _cp_cache_map.initialize(length / 2); coleenp@4037: // Also cache resolved objects, in another different cache. coleenp@4037: _reference_map.initialize(length, -1); coleenp@4037: _resolved_references_map.initialize(length / 2); coleenp@4037: _invokedynamic_references_map.initialize(length / 2); coleenp@4037: _resolved_reference_limit = -1; coleenp@4037: DEBUG_ONLY(_cp_cache_index_limit = -1); jrose@1161: } coleenp@4037: coleenp@4037: int _cp_cache_index_limit; coleenp@4037: void record_map_limits() { coleenp@4037: #ifdef ASSERT coleenp@4037: // Record initial size of the two arrays generated for the CP cache: coleenp@4037: _cp_cache_index_limit = _cp_cache_map.length(); coleenp@4037: #endif //ASSERT coleenp@4037: _resolved_reference_limit = _resolved_references_map.length(); coleenp@4037: } coleenp@4037: jrose@1161: int cp_entry_to_cp_cache(int i) { assert(has_cp_cache(i), "oob"); return _cp_map[i]; } jrose@1161: bool has_cp_cache(int i) { return (uint)i < (uint)_cp_map.length() && _cp_map[i] >= 0; } coleenp@4037: jrose@1161: int add_cp_cache_entry(int cp_index) { coleenp@4037: assert(_pool->tag_at(cp_index).value() != JVM_CONSTANT_InvokeDynamic, "use indy version"); jrose@1161: assert(_cp_map[cp_index] == -1, "not twice on same cp_index"); coleenp@4037: assert(_cp_cache_index_limit == -1, "do not add cache entries after first iteration"); jrose@1161: int cache_index = _cp_cache_map.append(cp_index); jrose@1161: _cp_map.at_put(cp_index, cache_index); jrose@1161: assert(cp_entry_to_cp_cache(cp_index) == cache_index, ""); coleenp@4037: assert(cp_cache_entry_pool_index(cache_index) == cp_index, ""); jrose@1161: return cache_index; jrose@1161: } coleenp@4037: coleenp@4037: // add a new CP cache entry beyond the normal cache (for invokedynamic only) coleenp@4037: int add_invokedynamic_cp_cache_entry(int cp_index) { coleenp@4037: assert(_pool->tag_at(cp_index).value() == JVM_CONSTANT_InvokeDynamic, "use non-indy version"); coleenp@4037: assert(_cp_map[cp_index] == -1, "do not map from cp_index"); coleenp@4037: assert(_cp_cache_index_limit >= 0, "add indy cache entries after first iteration"); coleenp@4037: int cache_index = _cp_cache_map.append(cp_index); coleenp@4037: assert(cache_index >= _cp_cache_index_limit, ""); coleenp@4037: // do not update _cp_map, since the mapping is one-to-many coleenp@4037: assert(cp_cache_entry_pool_index(cache_index) == cp_index, ""); jrose@1494: return cache_index; jrose@1494: } jrose@1161: coleenp@4037: // fix duplicated code later coleenp@4037: int cp_entry_to_resolved_references(int cp_index) const { coleenp@4037: assert(has_entry_in_resolved_references(cp_index), "oob"); coleenp@4037: return _reference_map[cp_index]; coleenp@4037: } coleenp@4037: bool has_entry_in_resolved_references(int cp_index) const { coleenp@4037: return (uint)cp_index < (uint)_reference_map.length() && _reference_map[cp_index] >= 0; coleenp@4037: } coleenp@4037: coleenp@4037: // add a new entry to the resolved_references map coleenp@4037: int add_resolved_references_entry(int cp_index) { coleenp@4037: assert(_reference_map[cp_index] == -1, "not twice on same cp_index"); coleenp@4037: assert(_resolved_reference_limit == -1, "do not add CP refs after first iteration"); coleenp@4037: int ref_index = _resolved_references_map.append(cp_index); coleenp@4037: _reference_map.at_put(cp_index, ref_index); coleenp@4037: assert(cp_entry_to_resolved_references(cp_index) == ref_index, ""); coleenp@4037: return ref_index; coleenp@4037: } coleenp@4037: twisti@4133: // add a new entries to the resolved_references map (for invokedynamic and invokehandle only) twisti@4133: int add_invokedynamic_resolved_references_entries(int cp_index, int cache_index) { coleenp@4037: assert(_resolved_reference_limit >= 0, "must add indy refs after first iteration"); twisti@4133: int ref_index = -1; twisti@4133: for (int entry = 0; entry < ConstantPoolCacheEntry::_indy_resolved_references_entries; entry++) { twisti@4133: const int index = _resolved_references_map.append(cp_index); // many-to-one twisti@4133: assert(index >= _resolved_reference_limit, ""); twisti@4133: if (entry == 0) { twisti@4133: ref_index = index; twisti@4133: } twisti@4133: assert((index - entry) == ref_index, "entries must be consecutive"); twisti@4133: _invokedynamic_references_map.at_put_grow(index, cache_index, -1); twisti@4133: } coleenp@4037: return ref_index; coleenp@4037: } coleenp@4037: coleenp@4037: int resolved_references_entry_to_pool_index(int ref_index) { coleenp@4037: int cp_index = _resolved_references_map[ref_index]; coleenp@4037: return cp_index; coleenp@4037: } coleenp@4037: jrose@2015: // Access the contents of _cp_cache_map to determine CP cache layout. jrose@2015: int cp_cache_entry_pool_index(int cache_index) { jrose@2015: int cp_index = _cp_cache_map[cache_index]; jrose@2015: return cp_index; jrose@2015: } jrose@2015: jrose@1161: // All the work goes in here: coleenp@4037: Rewriter(instanceKlassHandle klass, constantPoolHandle cpool, Array* methods, TRAPS); jrose@1161: jrose@1161: void compute_index_maps(); jrose@1161: void make_constant_pool_cache(TRAPS); coleenp@4037: void scan_method(Method* m, bool reverse = false); jrose@1161: void rewrite_Object_init(methodHandle m, TRAPS); coleenp@2945: void rewrite_member_reference(address bcp, int offset, bool reverse = false); coleenp@4037: void maybe_rewrite_invokehandle(address opc, int cp_index, int cache_index, bool reverse = false); coleenp@2945: void rewrite_invokedynamic(address bcp, int offset, bool reverse = false); coleenp@2945: void maybe_rewrite_ldc(address bcp, int offset, bool is_wide, bool reverse = false); coleenp@2945: // Revert bytecodes in case of an exception. coleenp@2945: void restore_bytecodes(); duke@435: coleenp@2945: static methodHandle rewrite_jsrs(methodHandle m, TRAPS); duke@435: public: jrose@1161: // Driver routine: duke@435: static void rewrite(instanceKlassHandle klass, TRAPS); duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_INTERPRETER_REWRITER_HPP