Path: blob/master/src/hotspot/share/interpreter/rewriter.hpp
40949 views
/*1* Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_INTERPRETER_REWRITER_HPP25#define SHARE_INTERPRETER_REWRITER_HPP2627#include "memory/allocation.hpp"28#include "utilities/growableArray.hpp"2930// The Rewriter adds caches to the constant pool and rewrites bytecode indices31// pointing into the constant pool for better interpreter performance.3233class Rewriter: public StackObj {34private:35InstanceKlass* _klass;36constantPoolHandle _pool;37Array<Method*>* _methods;38GrowableArray<int> _cp_map;39GrowableArray<int> _cp_cache_map; // for Methodref, Fieldref,40// InterfaceMethodref and InvokeDynamic41GrowableArray<int> _reference_map; // maps from cp index to resolved_refs index (or -1)42GrowableArray<int> _resolved_references_map; // for strings, methodHandle, methodType43GrowableArray<int> _invokedynamic_references_map; // for invokedynamic resolved refs44GrowableArray<int> _method_handle_invokers;45int _resolved_reference_limit;4647// For mapping invokedynamic bytecodes, which are discovered during method48// scanning. The invokedynamic entries are added at the end of the cpCache.49// If there are any invokespecial/InterfaceMethodref special case bytecodes,50// these entries are added before invokedynamic entries so that the51// invokespecial bytecode 16 bit index doesn't overflow.52GrowableArray<int> _invokedynamic_cp_cache_map;5354// For patching.55GrowableArray<address>* _patch_invokedynamic_bcps;56GrowableArray<int>* _patch_invokedynamic_refs;5758void init_maps(int length) {59_cp_map.trunc_to(0);60_cp_map.at_grow(length, -1);6162_cp_cache_map.trunc_to(0);63// Also cache resolved objects, in another different cache.64_reference_map.trunc_to(0);65_reference_map.at_grow(length, -1);6667_method_handle_invokers.trunc_to(0);68_resolved_references_map.trunc_to(0);69_invokedynamic_references_map.trunc_to(0);70_resolved_reference_limit = -1;71_first_iteration_cp_cache_limit = -1;7273// invokedynamic specific fields74_invokedynamic_cp_cache_map.trunc_to(0);75_patch_invokedynamic_bcps = new GrowableArray<address>(length / 4);76_patch_invokedynamic_refs = new GrowableArray<int>(length / 4);77}7879int _first_iteration_cp_cache_limit;80void record_map_limits() {81// Record initial size of the two arrays generated for the CP cache82// relative to walking the constant pool.83_first_iteration_cp_cache_limit = _cp_cache_map.length();84_resolved_reference_limit = _resolved_references_map.length();85}8687int cp_cache_delta() {88// How many cp cache entries were added since recording map limits after89// cp cache initialization?90assert(_first_iteration_cp_cache_limit != -1, "only valid after first iteration");91return _cp_cache_map.length() - _first_iteration_cp_cache_limit;92}9394int cp_entry_to_cp_cache(int i) { assert(has_cp_cache(i), "oob"); return _cp_map.at(i); }95bool has_cp_cache(int i) { return (uint) i < (uint) _cp_map.length() && _cp_map.at(i) >= 0; }9697int add_map_entry(int cp_index, GrowableArray<int>* cp_map, GrowableArray<int>* cp_cache_map) {98assert(cp_map->at(cp_index) == -1, "not twice on same cp_index");99int cache_index = cp_cache_map->append(cp_index);100cp_map->at_put(cp_index, cache_index);101return cache_index;102}103104int add_cp_cache_entry(int cp_index) {105assert(_pool->tag_at(cp_index).value() != JVM_CONSTANT_InvokeDynamic, "use indy version");106assert(_first_iteration_cp_cache_limit == -1, "do not add cache entries after first iteration");107int cache_index = add_map_entry(cp_index, &_cp_map, &_cp_cache_map);108assert(cp_entry_to_cp_cache(cp_index) == cache_index, "");109assert(cp_cache_entry_pool_index(cache_index) == cp_index, "");110return cache_index;111}112113int add_invokedynamic_cp_cache_entry(int cp_index) {114assert(_pool->tag_at(cp_index).value() == JVM_CONSTANT_InvokeDynamic, "use non-indy version");115assert(_first_iteration_cp_cache_limit >= 0, "add indy cache entries after first iteration");116// add to the invokedynamic index map.117int cache_index = _invokedynamic_cp_cache_map.append(cp_index);118// do not update _cp_map, since the mapping is one-to-many119assert(invokedynamic_cp_cache_entry_pool_index(cache_index) == cp_index, "");120// this index starts at one but in the bytecode it's appended to the end.121return cache_index + _first_iteration_cp_cache_limit;122}123124int invokedynamic_cp_cache_entry_pool_index(int cache_index) {125int cp_index = _invokedynamic_cp_cache_map.at(cache_index);126return cp_index;127}128129// add a new CP cache entry beyond the normal cache for the special case of130// invokespecial with InterfaceMethodref as cpool operand.131int add_invokespecial_cp_cache_entry(int cp_index) {132assert(_first_iteration_cp_cache_limit >= 0, "add these special cache entries after first iteration");133// Don't add InterfaceMethodref if it already exists at the end.134for (int i = _first_iteration_cp_cache_limit; i < _cp_cache_map.length(); i++) {135if (cp_cache_entry_pool_index(i) == cp_index) {136return i;137}138}139int cache_index = _cp_cache_map.append(cp_index);140assert(cache_index >= _first_iteration_cp_cache_limit, "");141// do not update _cp_map, since the mapping is one-to-many142assert(cp_cache_entry_pool_index(cache_index) == cp_index, "");143return cache_index;144}145146int cp_entry_to_resolved_references(int cp_index) const {147assert(has_entry_in_resolved_references(cp_index), "oob");148return _reference_map.at(cp_index);149}150bool has_entry_in_resolved_references(int cp_index) const {151return (uint) cp_index < (uint) _reference_map.length() && _reference_map.at(cp_index) >= 0;152}153154// add a new entry to the resolved_references map155int add_resolved_references_entry(int cp_index) {156int ref_index = add_map_entry(cp_index, &_reference_map, &_resolved_references_map);157assert(cp_entry_to_resolved_references(cp_index) == ref_index, "");158return ref_index;159}160161// add a new entry to the resolved_references map (for invokedynamic and invokehandle only)162int add_invokedynamic_resolved_references_entry(int cp_index, int cache_index) {163assert(_resolved_reference_limit >= 0, "must add indy refs after first iteration");164int ref_index = _resolved_references_map.append(cp_index); // many-to-one165assert(ref_index >= _resolved_reference_limit, "");166_invokedynamic_references_map.at_put_grow(ref_index, cache_index, -1);167return ref_index;168}169170int resolved_references_entry_to_pool_index(int ref_index) {171int cp_index = _resolved_references_map.at(ref_index);172return cp_index;173}174175// Access the contents of _cp_cache_map to determine CP cache layout.176int cp_cache_entry_pool_index(int cache_index) {177int cp_index = _cp_cache_map.at(cache_index);178return cp_index;179}180181// All the work goes in here:182Rewriter(InstanceKlass* klass, const constantPoolHandle& cpool, Array<Method*>* methods, TRAPS);183184void compute_index_maps();185void make_constant_pool_cache(TRAPS);186void scan_method(Thread* thread, Method* m, bool reverse, bool* invokespecial_error);187void rewrite_Object_init(const methodHandle& m, TRAPS);188void rewrite_member_reference(address bcp, int offset, bool reverse);189void maybe_rewrite_invokehandle(address opc, int cp_index, int cache_index, bool reverse);190void rewrite_invokedynamic(address bcp, int offset, bool reverse);191void maybe_rewrite_ldc(address bcp, int offset, bool is_wide, bool reverse);192void rewrite_invokespecial(address bcp, int offset, bool reverse, bool* invokespecial_error);193194void patch_invokedynamic_bytecodes();195196// Do all the work.197void rewrite_bytecodes(TRAPS);198199// Revert bytecodes in case of an exception.200void restore_bytecodes(Thread* thread);201202static methodHandle rewrite_jsrs(const methodHandle& m, TRAPS);203public:204// Driver routine:205static void rewrite(InstanceKlass* klass, TRAPS);206};207208#endif // SHARE_INTERPRETER_REWRITER_HPP209210211