Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/interpreter/rewriter.hpp
32285 views
/*1* Copyright (c) 1998, 2014, 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_VM_INTERPRETER_REWRITER_HPP25#define SHARE_VM_INTERPRETER_REWRITER_HPP2627#include "memory/allocation.hpp"28#include "runtime/handles.inline.hpp"29#include "utilities/growableArray.hpp"3031// The Rewriter adds caches to the constant pool and rewrites bytecode indices32// pointing into the constant pool for better interpreter performance.3334class Rewriter: public StackObj {35private:36instanceKlassHandle _klass;37constantPoolHandle _pool;38Array<Method*>* _methods;39intArray _cp_map;40intStack _cp_cache_map; // for Methodref, Fieldref,41// InterfaceMethodref and InvokeDynamic42intArray _reference_map; // maps from cp index to resolved_refs index (or -1)43intStack _resolved_references_map; // for strings, methodHandle, methodType44intStack _invokedynamic_references_map; // for invokedynamic resolved refs45intArray _method_handle_invokers;46int _resolved_reference_limit;4748// For mapping invokedynamic bytecodes, which are discovered during method49// scanning. The invokedynamic entries are added at the end of the cpCache.50// If there are any invokespecial/InterfaceMethodref special case bytecodes,51// these entries are added before invokedynamic entries so that the52// invokespecial bytecode 16 bit index doesn't overflow.53intStack _invokedynamic_cp_cache_map;5455// For patching.56GrowableArray<address>* _patch_invokedynamic_bcps;57GrowableArray<int>* _patch_invokedynamic_refs;5859void init_maps(int length) {60_cp_map.initialize(length, -1);61// Choose an initial value large enough that we don't get frequent62// calls to grow().63_cp_cache_map.initialize(length/2);64// Also cache resolved objects, in another different cache.65_reference_map.initialize(length, -1);66_resolved_references_map.initialize(length/2);67_invokedynamic_references_map.initialize(length/2);68_resolved_reference_limit = -1;69_first_iteration_cp_cache_limit = -1;7071// invokedynamic specific fields72_invokedynamic_cp_cache_map.initialize(length/4);73_patch_invokedynamic_bcps = new GrowableArray<address>(length/4);74_patch_invokedynamic_refs = new GrowableArray<int>(length/4);75}7677int _first_iteration_cp_cache_limit;78void record_map_limits() {79// Record initial size of the two arrays generated for the CP cache80// relative to walking the constant pool.81_first_iteration_cp_cache_limit = _cp_cache_map.length();82_resolved_reference_limit = _resolved_references_map.length();83}8485int cp_cache_delta() {86// How many cp cache entries were added since recording map limits after87// cp cache initialization?88assert(_first_iteration_cp_cache_limit != -1, "only valid after first iteration");89return _cp_cache_map.length() - _first_iteration_cp_cache_limit;90}9192int cp_entry_to_cp_cache(int i) { assert(has_cp_cache(i), "oob"); return _cp_map[i]; }93bool has_cp_cache(int i) { return (uint)i < (uint)_cp_map.length() && _cp_map[i] >= 0; }9495int add_map_entry(int cp_index, intArray* cp_map, intStack* cp_cache_map) {96assert(cp_map->at(cp_index) == -1, "not twice on same cp_index");97int cache_index = cp_cache_map->append(cp_index);98cp_map->at_put(cp_index, cache_index);99return cache_index;100}101102int add_cp_cache_entry(int cp_index) {103assert(_pool->tag_at(cp_index).value() != JVM_CONSTANT_InvokeDynamic, "use indy version");104assert(_first_iteration_cp_cache_limit == -1, "do not add cache entries after first iteration");105int cache_index = add_map_entry(cp_index, &_cp_map, &_cp_cache_map);106assert(cp_entry_to_cp_cache(cp_index) == cache_index, "");107assert(cp_cache_entry_pool_index(cache_index) == cp_index, "");108return cache_index;109}110111int add_invokedynamic_cp_cache_entry(int cp_index) {112assert(_pool->tag_at(cp_index).value() == JVM_CONSTANT_InvokeDynamic, "use non-indy version");113assert(_first_iteration_cp_cache_limit >= 0, "add indy cache entries after first iteration");114// add to the invokedynamic index map.115int cache_index = _invokedynamic_cp_cache_map.append(cp_index);116// do not update _cp_map, since the mapping is one-to-many117assert(invokedynamic_cp_cache_entry_pool_index(cache_index) == cp_index, "");118// this index starts at one but in the bytecode it's appended to the end.119return cache_index + _first_iteration_cp_cache_limit;120}121122int invokedynamic_cp_cache_entry_pool_index(int cache_index) {123int cp_index = _invokedynamic_cp_cache_map[cache_index];124return cp_index;125}126127// add a new CP cache entry beyond the normal cache for the special case of128// invokespecial with InterfaceMethodref as cpool operand.129int add_invokespecial_cp_cache_entry(int cp_index) {130assert(_first_iteration_cp_cache_limit >= 0, "add these special cache entries after first iteration");131// Don't add InterfaceMethodref if it already exists at the end.132for (int i = _first_iteration_cp_cache_limit; i < _cp_cache_map.length(); i++) {133if (cp_cache_entry_pool_index(i) == cp_index) {134return i;135}136}137int cache_index = _cp_cache_map.append(cp_index);138assert(cache_index >= _first_iteration_cp_cache_limit, "");139// do not update _cp_map, since the mapping is one-to-many140assert(cp_cache_entry_pool_index(cache_index) == cp_index, "");141return cache_index;142}143144int cp_entry_to_resolved_references(int cp_index) const {145assert(has_entry_in_resolved_references(cp_index), "oob");146return _reference_map[cp_index];147}148bool has_entry_in_resolved_references(int cp_index) const {149return (uint)cp_index < (uint)_reference_map.length() && _reference_map[cp_index] >= 0;150}151152// add a new entry to the resolved_references map153int add_resolved_references_entry(int cp_index) {154int ref_index = add_map_entry(cp_index, &_reference_map, &_resolved_references_map);155assert(cp_entry_to_resolved_references(cp_index) == ref_index, "");156return ref_index;157}158159// add a new entries to the resolved_references map (for invokedynamic and invokehandle only)160int add_invokedynamic_resolved_references_entries(int cp_index, int cache_index) {161assert(_resolved_reference_limit >= 0, "must add indy refs after first iteration");162int ref_index = -1;163for (int entry = 0; entry < ConstantPoolCacheEntry::_indy_resolved_references_entries; entry++) {164const int index = _resolved_references_map.append(cp_index); // many-to-one165assert(index >= _resolved_reference_limit, "");166if (entry == 0) {167ref_index = index;168}169assert((index - entry) == ref_index, "entries must be consecutive");170_invokedynamic_references_map.at_put_grow(index, cache_index, -1);171}172return ref_index;173}174175int resolved_references_entry_to_pool_index(int ref_index) {176int cp_index = _resolved_references_map[ref_index];177return cp_index;178}179180// Access the contents of _cp_cache_map to determine CP cache layout.181int cp_cache_entry_pool_index(int cache_index) {182int cp_index = _cp_cache_map[cache_index];183return cp_index;184}185186// All the work goes in here:187Rewriter(instanceKlassHandle klass, constantPoolHandle cpool, Array<Method*>* methods, TRAPS);188189void compute_index_maps();190void make_constant_pool_cache(TRAPS);191void scan_method(Method* m, bool reverse, bool* invokespecial_error);192void rewrite_Object_init(methodHandle m, TRAPS);193void rewrite_member_reference(address bcp, int offset, bool reverse);194void maybe_rewrite_invokehandle(address opc, int cp_index, int cache_index, bool reverse);195void rewrite_invokedynamic(address bcp, int offset, bool reverse);196void maybe_rewrite_ldc(address bcp, int offset, bool is_wide, bool reverse);197void rewrite_invokespecial(address bcp, int offset, bool reverse, bool* invokespecial_error);198199void patch_invokedynamic_bytecodes();200201// Do all the work.202void rewrite_bytecodes(TRAPS);203204// Revert bytecodes in case of an exception.205void restore_bytecodes();206207static methodHandle rewrite_jsrs(methodHandle m, TRAPS);208public:209// Driver routine:210static void rewrite(instanceKlassHandle klass, TRAPS);211};212213#endif // SHARE_VM_INTERPRETER_REWRITER_HPP214215216