Path: blob/master/src/hotspot/share/code/stubs.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_STUBS_HPP25#define SHARE_CODE_STUBS_HPP2627#include "asm/codeBuffer.hpp"28#include "memory/allocation.hpp"2930// The classes in this file provide a simple framework for the31// management of little pieces of machine code - or stubs -32// created on the fly and frequently discarded. In this frame-33// work stubs are stored in a queue.343536// Stub serves as abstract base class. A concrete stub37// implementation is a subclass of Stub, implementing38// all (non-virtual!) functions required sketched out39// in the Stub class.40//41// A concrete stub layout may look like this (both data42// and code sections could be empty as well):43//44// ________45// stub -->| | <--+46// | data | |47// |________| |48// code_begin -->| | |49// | | |50// | code | | size51// | | |52// |________| |53// code_end -->| | |54// | data | |55// |________| |56// <--+575859class Stub {60public:61// Initialization/finalization62void initialize(int size,63CodeStrings& strings) { ShouldNotCallThis(); } // called to initialize/specify the stub's size64void finalize() { ShouldNotCallThis(); } // called before the stub is deallocated6566// General info/converters67int size() const { ShouldNotCallThis(); return 0; } // must return the size provided by initialize68static int code_size_to_size(int code_size) { ShouldNotCallThis(); return 0; } // computes the size given the code size6970// Code info71address code_begin() const { ShouldNotCallThis(); return NULL; } // points to the first byte of the code72address code_end() const { ShouldNotCallThis(); return NULL; } // points to the first byte after the code7374// Debugging75void verify() { ShouldNotCallThis(); } // verifies the Stub76void print() { ShouldNotCallThis(); } // prints some information about the stub77};787980// A stub interface defines the interface between a stub queue81// and the stubs it queues. In order to avoid a vtable and82// (and thus the extra word) in each stub, a concrete stub83// interface object is created and associated with a stub84// buffer which in turn uses the stub interface to interact85// with its stubs.86//87// StubInterface serves as an abstract base class. A concrete88// stub interface implementation is a subclass of StubInterface,89// forwarding its virtual function calls to non-virtual calls90// of the concrete stub (see also macro below). There's exactly91// one stub interface instance required per stub queue.9293class StubInterface: public CHeapObj<mtCode> {94public:95// Initialization/finalization96virtual void initialize(Stub* self, int size,97CodeStrings& strings) = 0; // called after creation (called twice if allocated via (request, commit))98virtual void finalize(Stub* self) = 0; // called before deallocation99100// General info/converters101virtual int size(Stub* self) const = 0; // the total size of the stub in bytes (must be a multiple of CodeEntryAlignment)102virtual int code_size_to_size(int code_size) const = 0; // computes the total stub size in bytes given the code size in bytes103104// Code info105virtual address code_begin(Stub* self) const = 0; // points to the first code byte106virtual address code_end(Stub* self) const = 0; // points to the first byte after the code107108// Debugging109virtual void verify(Stub* self) = 0; // verifies the stub110virtual void print(Stub* self) = 0; // prints information about the stub111};112113114// DEF_STUB_INTERFACE is used to create a concrete stub interface115// class, forwarding stub interface calls to the corresponding116// stub calls.117118#define DEF_STUB_INTERFACE(stub) \119class stub##Interface: public StubInterface { \120private: \121static stub* cast(Stub* self) { return (stub*)self; } \122\123public: \124/* Initialization/finalization */ \125virtual void initialize(Stub* self, int size, \126CodeStrings& strings) { cast(self)->initialize(size, strings); } \127virtual void finalize(Stub* self) { cast(self)->finalize(); } \128\129/* General info */ \130virtual int size(Stub* self) const { return cast(self)->size(); } \131virtual int code_size_to_size(int code_size) const { return stub::code_size_to_size(code_size); } \132\133/* Code info */ \134virtual address code_begin(Stub* self) const { return cast(self)->code_begin(); } \135virtual address code_end(Stub* self) const { return cast(self)->code_end(); } \136\137/* Debugging */ \138virtual void verify(Stub* self) { cast(self)->verify(); } \139virtual void print(Stub* self) { cast(self)->print(); } \140};141142143// A StubQueue maintains a queue of stubs.144// Note: All sizes (spaces) are given in bytes.145146class StubQueue: public CHeapObj<mtCode> {147friend class VMStructs;148private:149StubInterface* _stub_interface; // the interface prototype150address _stub_buffer; // where all stubs are stored151int _buffer_size; // the buffer size in bytes152int _buffer_limit; // the (byte) index of the actual buffer limit (_buffer_limit <= _buffer_size)153int _queue_begin; // the (byte) index of the first queue entry (word-aligned)154int _queue_end; // the (byte) index of the first entry after the queue (word-aligned)155int _number_of_stubs; // the number of buffered stubs156Mutex* const _mutex; // the lock used for a (request, commit) transaction157158void check_index(int i) const { assert(0 <= i && i < _buffer_limit && i % CodeEntryAlignment == 0, "illegal index"); }159bool is_contiguous() const { return _queue_begin <= _queue_end; }160int index_of(Stub* s) const { int i = (address)s - _stub_buffer; check_index(i); return i; }161Stub* stub_at(int i) const { check_index(i); return (Stub*)(_stub_buffer + i); }162Stub* current_stub() const { return stub_at(_queue_end); }163164// Stub functionality accessed via interface165void stub_initialize(Stub* s, int size,166CodeStrings& strings) { assert(size % CodeEntryAlignment == 0, "size not aligned"); _stub_interface->initialize(s, size, strings); }167void stub_finalize(Stub* s) { _stub_interface->finalize(s); }168int stub_size(Stub* s) const { return _stub_interface->size(s); }169bool stub_contains(Stub* s, address pc) const { return _stub_interface->code_begin(s) <= pc && pc < _stub_interface->code_end(s); }170int stub_code_size_to_size(int code_size) const { return _stub_interface->code_size_to_size(code_size); }171void stub_verify(Stub* s) { _stub_interface->verify(s); }172void stub_print(Stub* s) { _stub_interface->print(s); }173174public:175StubQueue(StubInterface* stub_interface, int buffer_size, Mutex* lock,176const char* name);177~StubQueue();178179// General queue info180bool is_empty() const { return _queue_begin == _queue_end; }181int total_space() const { return _buffer_size - 1; }182int available_space() const { int d = _queue_begin - _queue_end - 1; return d < 0 ? d + _buffer_size : d; }183int used_space() const { return total_space() - available_space(); }184int number_of_stubs() const { return _number_of_stubs; }185bool contains(address pc) const { return _stub_buffer <= pc && pc < _stub_buffer + _buffer_limit; }186Stub* stub_containing(address pc) const;187address code_start() const { return _stub_buffer; }188address code_end() const { return _stub_buffer + _buffer_limit; }189190// Stub allocation (atomic transactions)191Stub* request_committed(int code_size); // request a stub that provides exactly code_size space for code192Stub* request(int requested_code_size); // request a stub with a (maximum) code space - locks the queue193void commit (int committed_code_size,194CodeStrings& strings); // commit the previously requested stub - unlocks the queue195196// Stub deallocation197void remove_first(); // remove the first stub in the queue198void remove_first(int n); // remove the first n stubs in the queue199void remove_all(); // remove all stubs in the queue200201void deallocate_unused_tail(); // deallocate the unused tail of the underlying CodeBlob202// only used from TemplateInterpreter::initialize()203// Iteration204Stub* first() const { return number_of_stubs() > 0 ? stub_at(_queue_begin) : NULL; }205Stub* next(Stub* s) const { int i = index_of(s) + stub_size(s);206// Only wrap around in the non-contiguous case (see stubss.cpp)207if (i == _buffer_limit && _queue_end < _buffer_limit) i = 0;208return (i == _queue_end) ? NULL : stub_at(i);209}210211// Debugging/printing212void verify(); // verifies the stub queue213void print(); // prints information about the stub queue214215};216217#endif // SHARE_CODE_STUBS_HPP218219220