Path: blob/master/src/hotspot/share/code/vtableStubs.hpp
40931 views
/*1* Copyright (c) 1997, 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_CODE_VTABLESTUBS_HPP25#define SHARE_CODE_VTABLESTUBS_HPP2627#include "asm/macroAssembler.hpp"28#include "code/vmreg.hpp"29#include "memory/allocation.hpp"3031// A VtableStub holds an individual code stub for a pair (vtable index, #args) for either itables or vtables32// There's a one-to-one relationship between a VtableStub and such a pair.3334// A word on VtableStub sizing:35// Such a vtable/itable stub consists of the instance data36// and an immediately following CodeBuffer.37// Unfortunately, the required space for the code buffer varies, depending on38// the setting of compile time macros (PRODUCT, ASSERT, ...) and of command line39// parameters. Actual data may have an influence on the size as well.40//41// A simple approximation for the VtableStub size would be to just take a value42// "large enough" for all circumstances - a worst case estimate.43// As there can exist many stubs - and they never go away - we certainly don't44// want to waste more code cache space than absolutely necessary.45//46// We need a different approach which, as far as possible, should be independent47// from or adaptive to code size variations. These variations may be caused by48// changed compile time or run time switches as well as by changed emitter code.49//50// Here is the idea:51// For the first stub we generate, we allocate a "large enough" code buffer.52// Once all instructions are emitted, we know the actual size of the stub.53// Remembering that size allows us to allocate a tightly matching code buffer54// for all subsequent stubs. That covers all "static variance", i.e. all variance55// that is due to compile time macros, command line parameters, machine capabilities,56// and other influences which are immutable for the life span of the vm.57//58// Life isn't always that easy. Code size may depend on actual data, "load constant"59// being an example for that. All code segments with such "dynamic variance" require60// additional care. We need to know or estimate the worst case code size for each61// such segment. With that knowledge, we can maintain a "slop counter" in the62// platform-specific stub emitters. It accumulates the difference between worst-case63// and actual code size. When the stub is fully generated, the actual stub size is64// adjusted (increased) by the slop counter value.65//66// As a result, we allocate all but the first code buffers with the same, tightly matching size.67//6869// VtableStubs creates the code stubs for compiled calls through vtables.70// There is one stub per (vtable index, args_size) pair, and the stubs are71// never deallocated. They don't need to be GCed because they contain no oops.72class VtableStub;7374class VtableStubs : AllStatic {75public: // N must be public (some compilers need this for _table)76enum {77N = 256, // size of stub table; must be power of two78mask = N - 179};8081private:82friend class VtableStub;83static VtableStub* _table[N]; // table of existing stubs84static int _number_of_vtable_stubs; // number of stubs created so far (for statistics)85static int _vtab_stub_size; // current size estimate for vtable stub (quasi-constant)86static int _itab_stub_size; // current size estimate for itable stub (quasi-constant)8788static VtableStub* create_vtable_stub(int vtable_index);89static VtableStub* create_itable_stub(int vtable_index);90static VtableStub* lookup (bool is_vtable_stub, int vtable_index);91static void enter (bool is_vtable_stub, int vtable_index, VtableStub* s);92static inline uint hash (bool is_vtable_stub, int vtable_index);93static address find_stub (bool is_vtable_stub, int vtable_index);94static void bookkeeping(MacroAssembler* masm, outputStream* out, VtableStub* s,95address npe_addr, address ame_addr, bool is_vtable_stub,96int index, int slop_bytes, int index_dependent_slop);97static int code_size_limit(bool is_vtable_stub);98static void check_and_set_size_limit(bool is_vtable_stub,99int code_size,100int padding);101102public:103static address find_vtable_stub(int vtable_index) { return find_stub(true, vtable_index); }104static address find_itable_stub(int itable_index) { return find_stub(false, itable_index); }105106static VtableStub* entry_point(address pc); // vtable stub entry point for a pc107static bool contains(address pc); // is pc within any stub?108static VtableStub* stub_containing(address pc); // stub containing pc or NULL109static int number_of_vtable_stubs() { return _number_of_vtable_stubs; }110static void initialize();111static void vtable_stub_do(void f(VtableStub*)); // iterates over all vtable stubs112};113114115class VtableStub {116private:117friend class VtableStubs;118119static address _chunk; // For allocation120static address _chunk_end; // For allocation121static VMReg _receiver_location; // Where to find receiver122123VtableStub* _next; // Pointer to next entry in hash table124const short _index; // vtable index125short _ame_offset; // Where an AbstractMethodError might occur126short _npe_offset; // Where a NullPointerException might occur127bool _is_vtable_stub; // True if vtable stub, false, is itable stub128/* code follows here */ // The vtableStub code129130void* operator new(size_t size, int code_size) throw();131132VtableStub(bool is_vtable_stub, int index)133: _next(NULL), _index(index), _ame_offset(-1), _npe_offset(-1),134_is_vtable_stub(is_vtable_stub) {}135VtableStub* next() const { return _next; }136int index() const { return _index; }137static VMReg receiver_location() { return _receiver_location; }138void set_next(VtableStub* n) { _next = n; }139140public:141address code_begin() const { return (address)(this + 1); }142address code_end() const { return code_begin() + VtableStubs::code_size_limit(_is_vtable_stub); }143address entry_point() const { return code_begin(); }144static int entry_offset() { return sizeof(class VtableStub); }145146bool matches(bool is_vtable_stub, int index) const {147return _index == index && _is_vtable_stub == is_vtable_stub;148}149bool contains(address pc) const { return code_begin() <= pc && pc < code_end(); }150151private:152void set_exception_points(address npe_addr, address ame_addr) {153_npe_offset = npe_addr - code_begin();154_ame_offset = ame_addr - code_begin();155assert(is_abstract_method_error(ame_addr), "offset must be correct");156assert(is_null_pointer_exception(npe_addr), "offset must be correct");157assert(!is_abstract_method_error(npe_addr), "offset must be correct");158assert(!is_null_pointer_exception(ame_addr), "offset must be correct");159}160161// platform-dependent routines162static int pd_code_alignment();163// CNC: Removed because vtable stubs are now made with an ideal graph164// static bool pd_disregard_arg_size();165166static void align_chunk() {167uintptr_t off = (uintptr_t)( _chunk + sizeof(VtableStub) ) % pd_code_alignment();168if (off != 0) _chunk += pd_code_alignment() - off;169}170171public:172// Query173bool is_itable_stub() { return !_is_vtable_stub; }174bool is_vtable_stub() { return _is_vtable_stub; }175bool is_abstract_method_error(address epc) { return epc == code_begin()+_ame_offset; }176bool is_null_pointer_exception(address epc) { return epc == code_begin()+_npe_offset; }177178void print_on(outputStream* st) const;179void print() const;180181};182183#endif // SHARE_CODE_VTABLESTUBS_HPP184185186