duke@435: /* twisti@1570: * Copyright 1998-2009 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: // CodeBlob - superclass for all entries in the CodeCache. duke@435: // duke@435: // Suptypes are: duke@435: // nmethod : Compiled Java methods (include method that calls to native code) duke@435: // RuntimeStub : Call to VM runtime methods duke@435: // DeoptimizationBlob : Used for deoptimizatation duke@435: // ExceptionBlob : Used for stack unrolling duke@435: // SafepointBlob : Used to handle illegal instruction exceptions duke@435: // duke@435: // duke@435: // Layout: duke@435: // - header duke@435: // - relocation duke@435: // - instruction space duke@435: // - data space duke@435: class DeoptimizationBlob; duke@435: duke@435: class CodeBlob VALUE_OBJ_CLASS_SPEC { duke@435: duke@435: friend class VMStructs; duke@435: duke@435: private: duke@435: const char* _name; duke@435: int _size; // total size of CodeBlob in bytes duke@435: int _header_size; // size of header (depends on subclass) duke@435: int _relocation_size; // size of relocation duke@435: int _instructions_offset; // offset to where instructions region begins duke@435: int _frame_complete_offset; // instruction offsets in [0.._frame_complete_offset) have duke@435: // not finished setting up their frame. Beware of pc's in duke@435: // that range. There is a similar range(s) on returns duke@435: // which we don't detect. duke@435: int _data_offset; // offset to where data region begins duke@435: int _oops_offset; // offset to where embedded oop table begins (inside data) duke@435: int _oops_length; // number of embedded oops duke@435: int _frame_size; // size of stack frame duke@435: OopMapSet* _oop_maps; // OopMap for this CodeBlob duke@435: CodeComments _comments; duke@435: duke@435: friend class OopRecorder; duke@435: duke@435: void fix_oop_relocations(address begin, address end, bool initialize_immediates); duke@435: inline void initialize_immediate_oop(oop* dest, jobject handle); duke@435: duke@435: public: duke@435: // Returns the space needed for CodeBlob duke@435: static unsigned int allocation_size(CodeBuffer* cb, int header_size); duke@435: duke@435: // Creation duke@435: // a) simple CodeBlob duke@435: // frame_complete is the offset from the beginning of the instructions duke@435: // to where the frame setup (from stackwalk viewpoint) is complete. duke@435: CodeBlob(const char* name, int header_size, int size, int frame_complete, int locs_size); duke@435: duke@435: // b) full CodeBlob duke@435: CodeBlob( duke@435: const char* name, duke@435: CodeBuffer* cb, duke@435: int header_size, duke@435: int size, duke@435: int frame_complete, duke@435: int frame_size, duke@435: OopMapSet* oop_maps duke@435: ); duke@435: duke@435: // Deletion duke@435: void flush(); duke@435: duke@435: // Typing duke@435: virtual bool is_buffer_blob() const { return false; } duke@435: virtual bool is_nmethod() const { return false; } duke@435: virtual bool is_runtime_stub() const { return false; } duke@435: virtual bool is_deoptimization_stub() const { return false; } duke@435: virtual bool is_uncommon_trap_stub() const { return false; } duke@435: virtual bool is_exception_stub() const { return false; } duke@435: virtual bool is_safepoint_stub() const { return false; } duke@435: virtual bool is_adapter_blob() const { return false; } duke@435: duke@435: virtual bool is_compiled_by_c2() const { return false; } duke@435: virtual bool is_compiled_by_c1() const { return false; } duke@435: twisti@1570: // Casting twisti@1570: nmethod* as_nmethod_or_null() { return is_nmethod() ? (nmethod*) this : NULL; } twisti@1570: duke@435: // Boundaries duke@435: address header_begin() const { return (address) this; } duke@435: address header_end() const { return ((address) this) + _header_size; }; duke@435: relocInfo* relocation_begin() const { return (relocInfo*) header_end(); }; duke@435: relocInfo* relocation_end() const { return (relocInfo*)(header_end() + _relocation_size); } duke@435: address instructions_begin() const { return (address) header_begin() + _instructions_offset; } duke@435: address instructions_end() const { return (address) header_begin() + _data_offset; } duke@435: address data_begin() const { return (address) header_begin() + _data_offset; } duke@435: address data_end() const { return (address) header_begin() + _size; } duke@435: oop* oops_begin() const { return (oop*) (header_begin() + _oops_offset); } duke@435: oop* oops_end() const { return oops_begin() + _oops_length; } duke@435: duke@435: // Offsets duke@435: int relocation_offset() const { return _header_size; } duke@435: int instructions_offset() const { return _instructions_offset; } duke@435: int data_offset() const { return _data_offset; } duke@435: int oops_offset() const { return _oops_offset; } duke@435: duke@435: // Sizes duke@435: int size() const { return _size; } duke@435: int header_size() const { return _header_size; } duke@435: int relocation_size() const { return (address) relocation_end() - (address) relocation_begin(); } duke@435: int instructions_size() const { return instructions_end() - instructions_begin(); } duke@435: int data_size() const { return data_end() - data_begin(); } duke@435: int oops_size() const { return (address) oops_end() - (address) oops_begin(); } duke@435: duke@435: // Containment duke@435: bool blob_contains(address addr) const { return header_begin() <= addr && addr < data_end(); } duke@435: bool relocation_contains(relocInfo* addr) const{ return relocation_begin() <= addr && addr < relocation_end(); } duke@435: bool instructions_contains(address addr) const { return instructions_begin() <= addr && addr < instructions_end(); } duke@435: bool data_contains(address addr) const { return data_begin() <= addr && addr < data_end(); } duke@435: bool oops_contains(oop* addr) const { return oops_begin() <= addr && addr < oops_end(); } duke@435: bool contains(address addr) const { return instructions_contains(addr); } duke@435: bool is_frame_complete_at(address addr) const { return instructions_contains(addr) && duke@435: addr >= instructions_begin() + _frame_complete_offset; } duke@435: duke@435: // Relocation support duke@435: void fix_oop_relocations(address begin, address end) { duke@435: fix_oop_relocations(begin, end, false); duke@435: } duke@435: void fix_oop_relocations() { duke@435: fix_oop_relocations(NULL, NULL, false); duke@435: } duke@435: relocInfo::relocType reloc_type_for_address(address pc); duke@435: bool is_at_poll_return(address pc); duke@435: bool is_at_poll_or_poll_return(address pc); duke@435: duke@435: // Support for oops in scopes and relocs: duke@435: // Note: index 0 is reserved for null. duke@435: oop oop_at(int index) const { return index == 0? (oop)NULL: *oop_addr_at(index); } duke@435: oop* oop_addr_at(int index) const{ // for GC duke@435: // relocation indexes are biased by 1 (because 0 is reserved) duke@435: assert(index > 0 && index <= _oops_length, "must be a valid non-zero index"); duke@435: return &oops_begin()[index-1]; duke@435: } duke@435: duke@435: void copy_oops(GrowableArray* oops); duke@435: duke@435: // CodeCache support: really only used by the nmethods, but in order to get duke@435: // asserts and certain bookkeeping to work in the CodeCache they are defined duke@435: // virtual here. duke@435: virtual bool is_zombie() const { return false; } duke@435: virtual bool is_locked_by_vm() const { return false; } duke@435: duke@435: virtual bool is_unloaded() const { return false; } duke@435: virtual bool is_not_entrant() const { return false; } duke@435: duke@435: // GC support duke@435: virtual bool is_alive() const = 0; duke@435: virtual void do_unloading(BoolObjectClosure* is_alive, duke@435: OopClosure* keep_alive, duke@435: bool unloading_occurred); duke@435: virtual void oops_do(OopClosure* f) = 0; jrose@1424: // (All CodeBlob subtypes other than NMethod currently have jrose@1424: // an empty oops_do() method. duke@435: duke@435: // OopMap for frame duke@435: OopMapSet* oop_maps() const { return _oop_maps; } duke@435: void set_oop_maps(OopMapSet* p); duke@435: OopMap* oop_map_for_return_address(address return_address); duke@435: virtual void preserve_callee_argument_oops(frame fr, const RegisterMap* reg_map, OopClosure* f) { ShouldNotReachHere(); } duke@435: duke@435: // Frame support duke@435: int frame_size() const { return _frame_size; } duke@435: void set_frame_size(int size) { _frame_size = size; } duke@435: duke@435: // Returns true, if the next frame is responsible for GC'ing oops passed as arguments duke@435: virtual bool caller_must_gc_arguments(JavaThread* thread) const { return false; } duke@435: duke@435: // Naming duke@435: const char* name() const { return _name; } duke@435: void set_name(const char* name) { _name = name; } duke@435: duke@435: // Debugging duke@435: virtual void verify(); duke@435: virtual void print() const PRODUCT_RETURN; duke@435: virtual void print_value_on(outputStream* st) const PRODUCT_RETURN; duke@435: duke@435: // Print the comment associated with offset on stream, if there is one jrose@1590: virtual void print_block_comment(outputStream* stream, address block_begin) { jrose@1590: intptr_t offset = (intptr_t)(block_begin - instructions_begin()); duke@435: _comments.print_block_comment(stream, offset); duke@435: } duke@435: duke@435: // Transfer ownership of comments to this CodeBlob duke@435: void set_comments(CodeComments& comments) { duke@435: _comments.assign(comments); duke@435: } duke@435: }; duke@435: duke@435: duke@435: //---------------------------------------------------------------------------------------------------- duke@435: // BufferBlob: used to hold non-relocatable machine code such as the interpreter, stubroutines, etc. duke@435: duke@435: class BufferBlob: public CodeBlob { duke@435: friend class VMStructs; duke@435: private: duke@435: // Creation support duke@435: BufferBlob(const char* name, int size); duke@435: BufferBlob(const char* name, int size, CodeBuffer* cb); duke@435: duke@435: void* operator new(size_t s, unsigned size); duke@435: duke@435: public: duke@435: // Creation duke@435: static BufferBlob* create(const char* name, int buffer_size); duke@435: static BufferBlob* create(const char* name, CodeBuffer* cb); duke@435: duke@435: static void free(BufferBlob* buf); duke@435: duke@435: // Typing duke@435: bool is_buffer_blob() const { return true; } duke@435: bool is_adapter_blob() const; duke@435: duke@435: // GC/Verification support duke@435: void preserve_callee_argument_oops(frame fr, const RegisterMap* reg_map, OopClosure* f) { /* nothing to do */ } duke@435: bool is_alive() const { return true; } duke@435: void do_unloading(BoolObjectClosure* is_alive, duke@435: OopClosure* keep_alive, duke@435: bool unloading_occurred) { /* do nothing */ } duke@435: duke@435: void oops_do(OopClosure* f) { /* do nothing*/ } duke@435: duke@435: void verify(); duke@435: void print() const PRODUCT_RETURN; duke@435: void print_value_on(outputStream* st) const PRODUCT_RETURN; duke@435: }; duke@435: duke@435: duke@435: //---------------------------------------------------------------------------------------------------- duke@435: // RuntimeStub: describes stubs used by compiled code to call a (static) C++ runtime routine duke@435: duke@435: class RuntimeStub: public CodeBlob { duke@435: friend class VMStructs; duke@435: private: duke@435: bool _caller_must_gc_arguments; duke@435: duke@435: // Creation support duke@435: RuntimeStub( duke@435: const char* name, duke@435: CodeBuffer* cb, duke@435: int size, duke@435: int frame_complete, duke@435: int frame_size, duke@435: OopMapSet* oop_maps, duke@435: bool caller_must_gc_arguments duke@435: ); duke@435: duke@435: void* operator new(size_t s, unsigned size); duke@435: duke@435: public: duke@435: // Creation duke@435: static RuntimeStub* new_runtime_stub( duke@435: const char* stub_name, duke@435: CodeBuffer* cb, duke@435: int frame_complete, duke@435: int frame_size, duke@435: OopMapSet* oop_maps, duke@435: bool caller_must_gc_arguments duke@435: ); duke@435: duke@435: // Typing duke@435: bool is_runtime_stub() const { return true; } duke@435: duke@435: // GC support duke@435: bool caller_must_gc_arguments(JavaThread* thread) const { return _caller_must_gc_arguments; } duke@435: duke@435: address entry_point() { return instructions_begin(); } duke@435: duke@435: // GC/Verification support duke@435: void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f) { /* nothing to do */ } duke@435: bool is_alive() const { return true; } duke@435: void do_unloading(BoolObjectClosure* is_alive, duke@435: OopClosure* keep_alive, duke@435: bool unloading_occurred) { /* do nothing */ } duke@435: void oops_do(OopClosure* f) { /* do-nothing*/ } duke@435: duke@435: void verify(); duke@435: void print() const PRODUCT_RETURN; duke@435: void print_value_on(outputStream* st) const PRODUCT_RETURN; duke@435: }; duke@435: duke@435: duke@435: //---------------------------------------------------------------------------------------------------- duke@435: // Super-class for all blobs that exist in only one instance. Implements default behaviour. duke@435: duke@435: class SingletonBlob: public CodeBlob { duke@435: friend class VMStructs; duke@435: public: duke@435: SingletonBlob( duke@435: const char* name, duke@435: CodeBuffer* cb, duke@435: int header_size, duke@435: int size, duke@435: int frame_size, duke@435: OopMapSet* oop_maps duke@435: ) duke@435: : CodeBlob(name, cb, header_size, size, CodeOffsets::frame_never_safe, frame_size, oop_maps) duke@435: {}; duke@435: duke@435: bool is_alive() const { return true; } duke@435: void do_unloading(BoolObjectClosure* is_alive, duke@435: OopClosure* keep_alive, duke@435: bool unloading_occurred) { /* do-nothing*/ } duke@435: duke@435: void verify(); // does nothing duke@435: void print() const PRODUCT_RETURN; duke@435: void print_value_on(outputStream* st) const PRODUCT_RETURN; duke@435: }; duke@435: duke@435: duke@435: //---------------------------------------------------------------------------------------------------- duke@435: // DeoptimizationBlob duke@435: duke@435: class DeoptimizationBlob: public SingletonBlob { duke@435: friend class VMStructs; duke@435: private: duke@435: int _unpack_offset; duke@435: int _unpack_with_exception; duke@435: int _unpack_with_reexecution; duke@435: duke@435: int _unpack_with_exception_in_tls; duke@435: duke@435: // Creation support duke@435: DeoptimizationBlob( duke@435: CodeBuffer* cb, duke@435: int size, duke@435: OopMapSet* oop_maps, duke@435: int unpack_offset, duke@435: int unpack_with_exception_offset, duke@435: int unpack_with_reexecution_offset, duke@435: int frame_size duke@435: ); duke@435: duke@435: void* operator new(size_t s, unsigned size); duke@435: duke@435: public: duke@435: // Creation duke@435: static DeoptimizationBlob* create( duke@435: CodeBuffer* cb, duke@435: OopMapSet* oop_maps, duke@435: int unpack_offset, duke@435: int unpack_with_exception_offset, duke@435: int unpack_with_reexecution_offset, duke@435: int frame_size duke@435: ); duke@435: duke@435: // Typing duke@435: bool is_deoptimization_stub() const { return true; } duke@435: const DeoptimizationBlob *as_deoptimization_stub() const { return this; } duke@435: bool exception_address_is_unpack_entry(address pc) const { duke@435: address unpack_pc = unpack(); duke@435: return (pc == unpack_pc || (pc + frame::pc_return_offset) == unpack_pc); duke@435: } duke@435: duke@435: duke@435: duke@435: duke@435: // GC for args duke@435: void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f) { /* Nothing to do */ } duke@435: duke@435: // Iteration duke@435: void oops_do(OopClosure* f) {} duke@435: duke@435: // Printing duke@435: void print_value_on(outputStream* st) const PRODUCT_RETURN; duke@435: duke@435: address unpack() const { return instructions_begin() + _unpack_offset; } duke@435: address unpack_with_exception() const { return instructions_begin() + _unpack_with_exception; } duke@435: address unpack_with_reexecution() const { return instructions_begin() + _unpack_with_reexecution; } duke@435: duke@435: // Alternate entry point for C1 where the exception and issuing pc duke@435: // are in JavaThread::_exception_oop and JavaThread::_exception_pc duke@435: // instead of being in registers. This is needed because C1 doesn't duke@435: // model exception paths in a way that keeps these registers free so duke@435: // there may be live values in those registers during deopt. duke@435: void set_unpack_with_exception_in_tls_offset(int offset) { duke@435: _unpack_with_exception_in_tls = offset; duke@435: assert(contains(instructions_begin() + _unpack_with_exception_in_tls), "must be PC inside codeblob"); duke@435: } duke@435: address unpack_with_exception_in_tls() const { return instructions_begin() + _unpack_with_exception_in_tls; } duke@435: }; duke@435: duke@435: duke@435: //---------------------------------------------------------------------------------------------------- duke@435: // UncommonTrapBlob (currently only used by Compiler 2) duke@435: duke@435: #ifdef COMPILER2 duke@435: duke@435: class UncommonTrapBlob: public SingletonBlob { duke@435: friend class VMStructs; duke@435: private: duke@435: // Creation support duke@435: UncommonTrapBlob( duke@435: CodeBuffer* cb, duke@435: int size, duke@435: OopMapSet* oop_maps, duke@435: int frame_size duke@435: ); duke@435: duke@435: void* operator new(size_t s, unsigned size); duke@435: duke@435: public: duke@435: // Creation duke@435: static UncommonTrapBlob* create( duke@435: CodeBuffer* cb, duke@435: OopMapSet* oop_maps, duke@435: int frame_size duke@435: ); duke@435: duke@435: // GC for args duke@435: void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f) { /* nothing to do */ } duke@435: duke@435: // Typing duke@435: bool is_uncommon_trap_stub() const { return true; } duke@435: duke@435: // Iteration duke@435: void oops_do(OopClosure* f) {} duke@435: }; duke@435: duke@435: duke@435: //---------------------------------------------------------------------------------------------------- duke@435: // ExceptionBlob: used for exception unwinding in compiled code (currently only used by Compiler 2) duke@435: duke@435: class ExceptionBlob: public SingletonBlob { duke@435: friend class VMStructs; duke@435: private: duke@435: // Creation support duke@435: ExceptionBlob( duke@435: CodeBuffer* cb, duke@435: int size, duke@435: OopMapSet* oop_maps, duke@435: int frame_size duke@435: ); duke@435: duke@435: void* operator new(size_t s, unsigned size); duke@435: duke@435: public: duke@435: // Creation duke@435: static ExceptionBlob* create( duke@435: CodeBuffer* cb, duke@435: OopMapSet* oop_maps, duke@435: int frame_size duke@435: ); duke@435: duke@435: // GC for args duke@435: void preserve_callee_argument_oops(frame fr, const RegisterMap* reg_map, OopClosure* f) { /* nothing to do */ } duke@435: duke@435: // Typing duke@435: bool is_exception_stub() const { return true; } duke@435: duke@435: // Iteration duke@435: void oops_do(OopClosure* f) {} duke@435: }; duke@435: #endif // COMPILER2 duke@435: duke@435: duke@435: //---------------------------------------------------------------------------------------------------- duke@435: // SafepointBlob: handles illegal_instruction exceptions during a safepoint duke@435: duke@435: class SafepointBlob: public SingletonBlob { duke@435: friend class VMStructs; duke@435: private: duke@435: // Creation support duke@435: SafepointBlob( duke@435: CodeBuffer* cb, duke@435: int size, duke@435: OopMapSet* oop_maps, duke@435: int frame_size duke@435: ); duke@435: duke@435: void* operator new(size_t s, unsigned size); duke@435: duke@435: public: duke@435: // Creation duke@435: static SafepointBlob* create( duke@435: CodeBuffer* cb, duke@435: OopMapSet* oop_maps, duke@435: int frame_size duke@435: ); duke@435: duke@435: // GC for args duke@435: void preserve_callee_argument_oops(frame fr, const RegisterMap* reg_map, OopClosure* f) { /* nothing to do */ } duke@435: duke@435: // Typing duke@435: bool is_safepoint_stub() const { return true; } duke@435: duke@435: // Iteration duke@435: void oops_do(OopClosure* f) {} duke@435: };