Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/code/stubs.hpp
32285 views
/*1* Copyright (c) 1997, 2013, 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_CODE_STUBS_HPP25#define SHARE_VM_CODE_STUBS_HPP2627#include "asm/codeBuffer.hpp"28#include "memory/allocation.hpp"29#ifdef TARGET_OS_FAMILY_linux30# include "os_linux.inline.hpp"31#endif32#ifdef TARGET_OS_FAMILY_solaris33# include "os_solaris.inline.hpp"34#endif35#ifdef TARGET_OS_FAMILY_windows36# include "os_windows.inline.hpp"37#endif38#ifdef TARGET_OS_FAMILY_aix39# include "os_aix.inline.hpp"40#endif41#ifdef TARGET_OS_FAMILY_bsd42# include "os_bsd.inline.hpp"43#endif4445// The classes in this file provide a simple framework for the46// management of little pieces of machine code - or stubs -47// created on the fly and frequently discarded. In this frame-48// work stubs are stored in a queue.495051// Stub serves as abstract base class. A concrete stub52// implementation is a subclass of Stub, implementing53// all (non-virtual!) functions required sketched out54// in the Stub class.55//56// A concrete stub layout may look like this (both data57// and code sections could be empty as well):58//59// ________60// stub -->| | <--+61// | data | |62// |________| |63// code_begin -->| | |64// | | |65// | code | | size66// | | |67// |________| |68// code_end -->| | |69// | data | |70// |________| |71// <--+727374class Stub VALUE_OBJ_CLASS_SPEC {75public:76// Initialization/finalization77void initialize(int size,78CodeStrings& strings) { ShouldNotCallThis(); } // called to initialize/specify the stub's size79void finalize() { ShouldNotCallThis(); } // called before the stub is deallocated8081// General info/converters82int size() const { ShouldNotCallThis(); return 0; } // must return the size provided by initialize83static int code_size_to_size(int code_size) { ShouldNotCallThis(); return 0; } // computes the size given the code size8485// Code info86address code_begin() const { ShouldNotCallThis(); return NULL; } // points to the first byte of the code87address code_end() const { ShouldNotCallThis(); return NULL; } // points to the first byte after the code8889// Debugging90void verify() { ShouldNotCallThis(); } // verifies the Stub91void print() { ShouldNotCallThis(); } // prints some information about the stub92};939495// A stub interface defines the interface between a stub queue96// and the stubs it queues. In order to avoid a vtable and97// (and thus the extra word) in each stub, a concrete stub98// interface object is created and associated with a stub99// buffer which in turn uses the stub interface to interact100// with its stubs.101//102// StubInterface serves as an abstract base class. A concrete103// stub interface implementation is a subclass of StubInterface,104// forwarding its virtual function calls to non-virtual calls105// of the concrete stub (see also macro below). There's exactly106// one stub interface instance required per stub queue.107108class StubInterface: public CHeapObj<mtCode> {109public:110// Initialization/finalization111virtual void initialize(Stub* self, int size,112CodeStrings& strings) = 0; // called after creation (called twice if allocated via (request, commit))113virtual void finalize(Stub* self) = 0; // called before deallocation114115// General info/converters116virtual int size(Stub* self) const = 0; // the total size of the stub in bytes (must be a multiple of CodeEntryAlignment)117virtual int code_size_to_size(int code_size) const = 0; // computes the total stub size in bytes given the code size in bytes118119// Code info120virtual address code_begin(Stub* self) const = 0; // points to the first code byte121virtual address code_end(Stub* self) const = 0; // points to the first byte after the code122123// Debugging124virtual void verify(Stub* self) = 0; // verifies the stub125virtual void print(Stub* self) = 0; // prints information about the stub126};127128129// DEF_STUB_INTERFACE is used to create a concrete stub interface130// class, forwarding stub interface calls to the corresponding131// stub calls.132133#define DEF_STUB_INTERFACE(stub) \134class stub##Interface: public StubInterface { \135private: \136static stub* cast(Stub* self) { return (stub*)self; } \137\138public: \139/* Initialization/finalization */ \140virtual void initialize(Stub* self, int size, \141CodeStrings& strings) { cast(self)->initialize(size, strings); } \142virtual void finalize(Stub* self) { cast(self)->finalize(); } \143\144/* General info */ \145virtual int size(Stub* self) const { return cast(self)->size(); } \146virtual int code_size_to_size(int code_size) const { return stub::code_size_to_size(code_size); } \147\148/* Code info */ \149virtual address code_begin(Stub* self) const { return cast(self)->code_begin(); } \150virtual address code_end(Stub* self) const { return cast(self)->code_end(); } \151\152/* Debugging */ \153virtual void verify(Stub* self) { cast(self)->verify(); } \154virtual void print(Stub* self) { cast(self)->print(); } \155};156157158// A StubQueue maintains a queue of stubs.159// Note: All sizes (spaces) are given in bytes.160161class StubQueue: public CHeapObj<mtCode> {162friend class VMStructs;163private:164StubInterface* _stub_interface; // the interface prototype165address _stub_buffer; // where all stubs are stored166int _buffer_size; // the buffer size in bytes167int _buffer_limit; // the (byte) index of the actual buffer limit (_buffer_limit <= _buffer_size)168int _queue_begin; // the (byte) index of the first queue entry (word-aligned)169int _queue_end; // the (byte) index of the first entry after the queue (word-aligned)170int _number_of_stubs; // the number of buffered stubs171Mutex* const _mutex; // the lock used for a (request, commit) transaction172173void check_index(int i) const { assert(0 <= i && i < _buffer_limit && i % CodeEntryAlignment == 0, "illegal index"); }174bool is_contiguous() const { return _queue_begin <= _queue_end; }175int index_of(Stub* s) const { int i = (address)s - _stub_buffer; check_index(i); return i; }176Stub* stub_at(int i) const { check_index(i); return (Stub*)(_stub_buffer + i); }177Stub* current_stub() const { return stub_at(_queue_end); }178179// Stub functionality accessed via interface180void stub_initialize(Stub* s, int size,181CodeStrings& strings) { assert(size % CodeEntryAlignment == 0, "size not aligned"); _stub_interface->initialize(s, size, strings); }182void stub_finalize(Stub* s) { _stub_interface->finalize(s); }183int stub_size(Stub* s) const { return _stub_interface->size(s); }184bool stub_contains(Stub* s, address pc) const { return _stub_interface->code_begin(s) <= pc && pc < _stub_interface->code_end(s); }185int stub_code_size_to_size(int code_size) const { return _stub_interface->code_size_to_size(code_size); }186void stub_verify(Stub* s) { _stub_interface->verify(s); }187void stub_print(Stub* s) { _stub_interface->print(s); }188189static void register_queue(StubQueue*);190191public:192StubQueue(StubInterface* stub_interface, int buffer_size, Mutex* lock,193const char* name);194~StubQueue();195196// General queue info197bool is_empty() const { return _queue_begin == _queue_end; }198int total_space() const { return _buffer_size - 1; }199int available_space() const { int d = _queue_begin - _queue_end - 1; return d < 0 ? d + _buffer_size : d; }200int used_space() const { return total_space() - available_space(); }201int number_of_stubs() const { return _number_of_stubs; }202bool contains(address pc) const { return _stub_buffer <= pc && pc < _stub_buffer + _buffer_limit; }203Stub* stub_containing(address pc) const;204address code_start() const { return _stub_buffer; }205address code_end() const { return _stub_buffer + _buffer_limit; }206207// Stub allocation (atomic transactions)208Stub* request_committed(int code_size); // request a stub that provides exactly code_size space for code209Stub* request(int requested_code_size); // request a stub with a (maximum) code space - locks the queue210void commit (int committed_code_size,211CodeStrings& strings); // commit the previously requested stub - unlocks the queue212213// Stub deallocation214void remove_first(); // remove the first stub in the queue215void remove_first(int n); // remove the first n stubs in the queue216void remove_all(); // remove all stubs in the queue217218// Iteration219static void queues_do(void f(StubQueue* s)); // call f with each StubQueue220void stubs_do(void f(Stub* s)); // call f with all stubs221Stub* first() const { return number_of_stubs() > 0 ? stub_at(_queue_begin) : NULL; }222Stub* next(Stub* s) const { int i = index_of(s) + stub_size(s);223if (i == _buffer_limit) i = 0;224return (i == _queue_end) ? NULL : stub_at(i);225}226227address stub_code_begin(Stub* s) const { return _stub_interface->code_begin(s); }228address stub_code_end(Stub* s) const { return _stub_interface->code_end(s); }229230// Debugging/printing231void verify(); // verifies the stub queue232void print(); // prints information about the stub queue233};234235#endif // SHARE_VM_CODE_STUBS_HPP236237238