aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_ASM_CODEBUFFER_HPP aoqi@0: #define SHARE_VM_ASM_CODEBUFFER_HPP aoqi@0: aoqi@0: #include "code/oopRecorder.hpp" aoqi@0: #include "code/relocInfo.hpp" aoqi@0: aoqi@0: class CodeStrings; aoqi@0: class PhaseCFG; aoqi@0: class Compile; aoqi@0: class BufferBlob; aoqi@0: class CodeBuffer; aoqi@0: class Label; aoqi@0: aoqi@0: class CodeOffsets: public StackObj { aoqi@0: public: aoqi@0: enum Entries { Entry, aoqi@0: Verified_Entry, aoqi@0: Frame_Complete, // Offset in the code where the frame setup is (for forte stackwalks) is complete aoqi@0: OSR_Entry, aoqi@0: Dtrace_trap = OSR_Entry, // dtrace probes can never have an OSR entry so reuse it aoqi@0: Exceptions, // Offset where exception handler lives aoqi@0: Deopt, // Offset where deopt handler lives aoqi@0: DeoptMH, // Offset where MethodHandle deopt handler lives aoqi@0: UnwindHandler, // Offset to default unwind handler aoqi@0: max_Entries }; aoqi@0: aoqi@0: // special value to note codeBlobs where profile (forte) stack walking is aoqi@0: // always dangerous and suspect. aoqi@0: aoqi@0: enum { frame_never_safe = -1 }; aoqi@0: aoqi@0: private: aoqi@0: int _values[max_Entries]; aoqi@0: aoqi@0: public: aoqi@0: CodeOffsets() { aoqi@0: _values[Entry ] = 0; aoqi@0: _values[Verified_Entry] = 0; aoqi@0: _values[Frame_Complete] = frame_never_safe; aoqi@0: _values[OSR_Entry ] = 0; aoqi@0: _values[Exceptions ] = -1; aoqi@0: _values[Deopt ] = -1; aoqi@0: _values[DeoptMH ] = -1; aoqi@0: _values[UnwindHandler ] = -1; aoqi@0: } aoqi@0: aoqi@0: int value(Entries e) { return _values[e]; } aoqi@0: void set_value(Entries e, int val) { _values[e] = val; } aoqi@0: }; aoqi@0: aoqi@0: // This class represents a stream of code and associated relocations. aoqi@0: // There are a few in each CodeBuffer. aoqi@0: // They are filled concurrently, and concatenated at the end. aoqi@0: class CodeSection VALUE_OBJ_CLASS_SPEC { aoqi@0: friend class CodeBuffer; aoqi@0: public: aoqi@0: typedef int csize_t; // code size type; would be size_t except for history aoqi@0: aoqi@0: private: aoqi@0: address _start; // first byte of contents (instructions) aoqi@0: address _mark; // user mark, usually an instruction beginning aoqi@0: address _end; // current end address aoqi@0: address _limit; // last possible (allocated) end address aoqi@0: relocInfo* _locs_start; // first byte of relocation information aoqi@0: relocInfo* _locs_end; // first byte after relocation information aoqi@0: relocInfo* _locs_limit; // first byte after relocation information buf aoqi@0: address _locs_point; // last relocated position (grows upward) aoqi@0: bool _locs_own; // did I allocate the locs myself? aoqi@0: bool _frozen; // no more expansion of this section aoqi@0: char _index; // my section number (SECT_INST, etc.) aoqi@0: CodeBuffer* _outer; // enclosing CodeBuffer aoqi@0: aoqi@0: // (Note: _locs_point used to be called _last_reloc_offset.) aoqi@0: aoqi@0: CodeSection() { aoqi@0: _start = NULL; aoqi@0: _mark = NULL; aoqi@0: _end = NULL; aoqi@0: _limit = NULL; aoqi@0: _locs_start = NULL; aoqi@0: _locs_end = NULL; aoqi@0: _locs_limit = NULL; aoqi@0: _locs_point = NULL; aoqi@0: _locs_own = false; aoqi@0: _frozen = false; aoqi@0: debug_only(_index = (char)-1); aoqi@0: debug_only(_outer = (CodeBuffer*)badAddress); aoqi@0: } aoqi@0: aoqi@0: void initialize_outer(CodeBuffer* outer, int index) { aoqi@0: _outer = outer; aoqi@0: _index = index; aoqi@0: } aoqi@0: aoqi@0: void initialize(address start, csize_t size = 0) { aoqi@0: assert(_start == NULL, "only one init step, please"); aoqi@0: _start = start; aoqi@0: _mark = NULL; aoqi@0: _end = start; aoqi@0: aoqi@0: _limit = start + size; aoqi@0: _locs_point = start; aoqi@0: } aoqi@0: aoqi@0: void initialize_locs(int locs_capacity); aoqi@0: void expand_locs(int new_capacity); aoqi@0: void initialize_locs_from(const CodeSection* source_cs); aoqi@0: aoqi@0: // helper for CodeBuffer::expand() aoqi@0: void take_over_code_from(CodeSection* cs) { aoqi@0: _start = cs->_start; aoqi@0: _mark = cs->_mark; aoqi@0: _end = cs->_end; aoqi@0: _limit = cs->_limit; aoqi@0: _locs_point = cs->_locs_point; aoqi@0: } aoqi@0: aoqi@0: public: aoqi@0: address start() const { return _start; } aoqi@0: address mark() const { return _mark; } aoqi@0: address end() const { return _end; } aoqi@0: address limit() const { return _limit; } aoqi@0: csize_t size() const { return (csize_t)(_end - _start); } aoqi@0: csize_t mark_off() const { assert(_mark != NULL, "not an offset"); aoqi@0: return (csize_t)(_mark - _start); } aoqi@0: csize_t capacity() const { return (csize_t)(_limit - _start); } aoqi@0: csize_t remaining() const { return (csize_t)(_limit - _end); } aoqi@0: aoqi@0: relocInfo* locs_start() const { return _locs_start; } aoqi@0: relocInfo* locs_end() const { return _locs_end; } aoqi@0: int locs_count() const { return (int)(_locs_end - _locs_start); } aoqi@0: relocInfo* locs_limit() const { return _locs_limit; } aoqi@0: address locs_point() const { return _locs_point; } aoqi@0: csize_t locs_point_off() const{ return (csize_t)(_locs_point - _start); } aoqi@0: csize_t locs_capacity() const { return (csize_t)(_locs_limit - _locs_start); } aoqi@0: csize_t locs_remaining()const { return (csize_t)(_locs_limit - _locs_end); } aoqi@0: aoqi@0: int index() const { return _index; } aoqi@0: bool is_allocated() const { return _start != NULL; } aoqi@0: bool is_empty() const { return _start == _end; } aoqi@0: bool is_frozen() const { return _frozen; } aoqi@0: bool has_locs() const { return _locs_end != NULL; } aoqi@0: aoqi@0: CodeBuffer* outer() const { return _outer; } aoqi@0: aoqi@0: // is a given address in this section? (2nd version is end-inclusive) aoqi@0: bool contains(address pc) const { return pc >= _start && pc < _end; } aoqi@0: bool contains2(address pc) const { return pc >= _start && pc <= _end; } aoqi@0: bool allocates(address pc) const { return pc >= _start && pc < _limit; } aoqi@0: bool allocates2(address pc) const { return pc >= _start && pc <= _limit; } aoqi@0: aoqi@0: void set_end(address pc) { assert(allocates2(pc), err_msg("not in CodeBuffer memory: " PTR_FORMAT " <= " PTR_FORMAT " <= " INTPTR_FORMAT, p2i(_start), p2i(pc), p2i(_limit))); _end = pc; } aoqi@0: void set_mark(address pc) { assert(contains2(pc), "not in codeBuffer"); aoqi@0: _mark = pc; } aoqi@0: void set_mark_off(int offset) { assert(contains2(offset+_start),"not in codeBuffer"); aoqi@0: _mark = offset + _start; } aoqi@0: void set_mark() { _mark = _end; } aoqi@0: void clear_mark() { _mark = NULL; } aoqi@0: aoqi@0: void set_locs_end(relocInfo* p) { aoqi@0: assert(p <= locs_limit(), "locs data fits in allocated buffer"); aoqi@0: _locs_end = p; aoqi@0: } aoqi@0: void set_locs_point(address pc) { aoqi@0: assert(pc >= locs_point(), "relocation addr may not decrease"); aoqi@0: assert(allocates2(pc), "relocation addr must be in this section"); aoqi@0: _locs_point = pc; aoqi@0: } aoqi@0: aoqi@0: // Code emission aoqi@0: void emit_int8 ( int8_t x) { *((int8_t*) end()) = x; set_end(end() + sizeof(int8_t)); } aoqi@0: void emit_int16( int16_t x) { *((int16_t*) end()) = x; set_end(end() + sizeof(int16_t)); } aoqi@0: void emit_int32( int32_t x) { *((int32_t*) end()) = x; set_end(end() + sizeof(int32_t)); } aoqi@0: void emit_int64( int64_t x) { *((int64_t*) end()) = x; set_end(end() + sizeof(int64_t)); } aoqi@0: aoqi@0: void emit_float( jfloat x) { *((jfloat*) end()) = x; set_end(end() + sizeof(jfloat)); } aoqi@0: void emit_double(jdouble x) { *((jdouble*) end()) = x; set_end(end() + sizeof(jdouble)); } aoqi@0: void emit_address(address x) { *((address*) end()) = x; set_end(end() + sizeof(address)); } aoqi@0: aoqi@0: // Share a scratch buffer for relocinfo. (Hacky; saves a resource allocation.) aoqi@0: void initialize_shared_locs(relocInfo* buf, int length); aoqi@0: aoqi@0: // Manage labels and their addresses. aoqi@0: address target(Label& L, address branch_pc); aoqi@0: aoqi@0: // Emit a relocation. aoqi@0: void relocate(address at, RelocationHolder const& rspec, int format = 0); aoqi@0: void relocate(address at, relocInfo::relocType rtype, int format = 0) { aoqi@0: if (rtype != relocInfo::none) aoqi@0: relocate(at, Relocation::spec_simple(rtype), format); aoqi@0: } aoqi@0: aoqi@0: // alignment requirement for starting offset aoqi@0: // Requirements are that the instruction area and the aoqi@0: // stubs area must start on CodeEntryAlignment, and aoqi@0: // the ctable on sizeof(jdouble) aoqi@0: int alignment() const { return MAX2((int)sizeof(jdouble), (int)CodeEntryAlignment); } aoqi@0: aoqi@0: // Slop between sections, used only when allocating temporary BufferBlob buffers. aoqi@0: static csize_t end_slop() { return MAX2((int)sizeof(jdouble), (int)CodeEntryAlignment); } aoqi@0: aoqi@0: csize_t align_at_start(csize_t off) const { return (csize_t) align_size_up(off, alignment()); } aoqi@0: aoqi@0: // Mark a section frozen. Assign its remaining space to aoqi@0: // the following section. It will never expand after this point. aoqi@0: inline void freeze(); // { _outer->freeze_section(this); } aoqi@0: aoqi@0: // Ensure there's enough space left in the current section. aoqi@0: // Return true if there was an expansion. aoqi@0: bool maybe_expand_to_ensure_remaining(csize_t amount); aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: void decode(); aoqi@0: void dump(); aoqi@0: void print(const char* name); aoqi@0: #endif //PRODUCT aoqi@0: }; aoqi@0: aoqi@0: class CodeString; aoqi@0: class CodeStrings VALUE_OBJ_CLASS_SPEC { aoqi@0: private: aoqi@0: #ifndef PRODUCT aoqi@0: CodeString* _strings; aoqi@0: #endif aoqi@0: aoqi@0: CodeString* find(intptr_t offset) const; aoqi@0: CodeString* find_last(intptr_t offset) const; aoqi@0: aoqi@0: public: aoqi@0: CodeStrings() { aoqi@0: #ifndef PRODUCT aoqi@0: _strings = NULL; aoqi@0: #endif aoqi@0: } aoqi@0: aoqi@0: const char* add_string(const char * string) PRODUCT_RETURN_(return NULL;); aoqi@0: aoqi@0: void add_comment(intptr_t offset, const char * comment) PRODUCT_RETURN; aoqi@0: void print_block_comment(outputStream* stream, intptr_t offset) const PRODUCT_RETURN; aoqi@0: void assign(CodeStrings& other) PRODUCT_RETURN; aoqi@0: void free() PRODUCT_RETURN; aoqi@0: }; aoqi@0: aoqi@0: // A CodeBuffer describes a memory space into which assembly aoqi@0: // code is generated. This memory space usually occupies the aoqi@0: // interior of a single BufferBlob, but in some cases it may be aoqi@0: // an arbitrary span of memory, even outside the code cache. aoqi@0: // aoqi@0: // A code buffer comes in two variants: aoqi@0: // aoqi@0: // (1) A CodeBuffer referring to an already allocated piece of memory: aoqi@0: // This is used to direct 'static' code generation (e.g. for interpreter aoqi@0: // or stubroutine generation, etc.). This code comes with NO relocation aoqi@0: // information. aoqi@0: // aoqi@0: // (2) A CodeBuffer referring to a piece of memory allocated when the aoqi@0: // CodeBuffer is allocated. This is used for nmethod generation. aoqi@0: // aoqi@0: // The memory can be divided up into several parts called sections. aoqi@0: // Each section independently accumulates code (or data) an relocations. aoqi@0: // Sections can grow (at the expense of a reallocation of the BufferBlob aoqi@0: // and recopying of all active sections). When the buffered code is finally aoqi@0: // written to an nmethod (or other CodeBlob), the contents (code, data, aoqi@0: // and relocations) of the sections are padded to an alignment and concatenated. aoqi@0: // Instructions and data in one section can contain relocatable references to aoqi@0: // addresses in a sibling section. aoqi@0: aoqi@0: class CodeBuffer: public StackObj { aoqi@0: friend class CodeSection; aoqi@0: aoqi@0: private: aoqi@0: // CodeBuffers must be allocated on the stack except for a single aoqi@0: // special case during expansion which is handled internally. This aoqi@0: // is done to guarantee proper cleanup of resources. aoqi@0: void* operator new(size_t size) throw() { return ResourceObj::operator new(size); } aoqi@0: void operator delete(void* p) { ShouldNotCallThis(); } aoqi@0: aoqi@0: public: aoqi@0: typedef int csize_t; // code size type; would be size_t except for history aoqi@0: enum { aoqi@0: // Here is the list of all possible sections. The order reflects aoqi@0: // the final layout. aoqi@0: SECT_FIRST = 0, aoqi@0: SECT_CONSTS = SECT_FIRST, // Non-instruction data: Floats, jump tables, etc. aoqi@0: SECT_INSTS, // Executable instructions. aoqi@0: SECT_STUBS, // Outbound trampolines for supporting call sites. aoqi@0: SECT_LIMIT, SECT_NONE = -1 aoqi@0: }; aoqi@0: aoqi@0: private: aoqi@0: enum { aoqi@0: sect_bits = 2, // assert (SECT_LIMIT <= (1<index() == n || !cs->is_allocated(), "sanity"); aoqi@0: return cs; aoqi@0: } aoqi@0: const CodeSection* code_section(int n) const { // yucky const stuff aoqi@0: return ((CodeBuffer*)this)->code_section(n); aoqi@0: } aoqi@0: static const char* code_section_name(int n); aoqi@0: int section_index_of(address addr) const; aoqi@0: bool contains(address addr) const { aoqi@0: // handy for debugging aoqi@0: return section_index_of(addr) > SECT_NONE; aoqi@0: } aoqi@0: aoqi@0: // A stable mapping between 'locators' (small ints) and addresses. aoqi@0: static int locator_pos(int locator) { return locator >> sect_bits; } aoqi@0: static int locator_sect(int locator) { return locator & sect_mask; } aoqi@0: static int locator(int pos, int sect) { return (pos << sect_bits) | sect; } aoqi@0: int locator(address addr) const; aoqi@0: address locator_address(int locator) const; aoqi@0: aoqi@0: // Heuristic for pre-packing the taken/not-taken bit of a predicted branch. aoqi@0: bool is_backward_branch(Label& L); aoqi@0: aoqi@0: // Properties aoqi@0: const char* name() const { return _name; } aoqi@0: CodeBuffer* before_expand() const { return _before_expand; } aoqi@0: BufferBlob* blob() const { return _blob; } aoqi@0: void set_blob(BufferBlob* blob); aoqi@0: void free_blob(); // Free the blob, if we own one. aoqi@0: aoqi@0: // Properties relative to the insts section: aoqi@0: address insts_begin() const { return _insts.start(); } aoqi@0: address insts_end() const { return _insts.end(); } aoqi@0: void set_insts_end(address end) { _insts.set_end(end); } aoqi@0: address insts_limit() const { return _insts.limit(); } aoqi@0: address insts_mark() const { return _insts.mark(); } aoqi@0: void set_insts_mark() { _insts.set_mark(); } aoqi@0: void clear_insts_mark() { _insts.clear_mark(); } aoqi@0: aoqi@0: // is there anything in the buffer other than the current section? aoqi@0: bool is_pure() const { return insts_size() == total_content_size(); } aoqi@0: aoqi@0: // size in bytes of output so far in the insts sections aoqi@0: csize_t insts_size() const { return _insts.size(); } aoqi@0: aoqi@0: // same as insts_size(), except that it asserts there is no non-code here aoqi@0: csize_t pure_insts_size() const { assert(is_pure(), "no non-code"); aoqi@0: return insts_size(); } aoqi@0: // capacity in bytes of the insts sections aoqi@0: csize_t insts_capacity() const { return _insts.capacity(); } aoqi@0: aoqi@0: // number of bytes remaining in the insts section aoqi@0: csize_t insts_remaining() const { return _insts.remaining(); } aoqi@0: aoqi@0: // is a given address in the insts section? (2nd version is end-inclusive) aoqi@0: bool insts_contains(address pc) const { return _insts.contains(pc); } aoqi@0: bool insts_contains2(address pc) const { return _insts.contains2(pc); } aoqi@0: aoqi@0: // Record any extra oops required to keep embedded metadata alive aoqi@0: void finalize_oop_references(methodHandle method); aoqi@0: aoqi@0: // Allocated size in all sections, when aligned and concatenated aoqi@0: // (this is the eventual state of the content in its final aoqi@0: // CodeBlob). aoqi@0: csize_t total_content_size() const; aoqi@0: aoqi@0: // Combined offset (relative to start of first section) of given aoqi@0: // section, as eventually found in the final CodeBlob. aoqi@0: csize_t total_offset_of(CodeSection* cs) const; aoqi@0: aoqi@0: // allocated size of all relocation data, including index, rounded up aoqi@0: csize_t total_relocation_size() const; aoqi@0: aoqi@0: // allocated size of any and all recorded oops aoqi@0: csize_t total_oop_size() const { aoqi@0: OopRecorder* recorder = oop_recorder(); aoqi@0: return (recorder == NULL)? 0: recorder->oop_size(); aoqi@0: } aoqi@0: aoqi@0: // allocated size of any and all recorded metadata aoqi@0: csize_t total_metadata_size() const { aoqi@0: OopRecorder* recorder = oop_recorder(); aoqi@0: return (recorder == NULL)? 0: recorder->metadata_size(); aoqi@0: } aoqi@0: aoqi@0: // Configuration functions, called immediately after the CB is constructed. aoqi@0: // The section sizes are subtracted from the original insts section. aoqi@0: // Note: Call them in reverse section order, because each steals from insts. aoqi@0: void initialize_consts_size(csize_t size) { initialize_section_size(&_consts, size); } aoqi@0: void initialize_stubs_size(csize_t size) { initialize_section_size(&_stubs, size); } aoqi@0: // Override default oop recorder. aoqi@0: void initialize_oop_recorder(OopRecorder* r); aoqi@0: aoqi@0: OopRecorder* oop_recorder() const { return _oop_recorder; } aoqi@0: CodeStrings& strings() { return _strings; } aoqi@0: aoqi@0: // Code generation aoqi@0: void relocate(address at, RelocationHolder const& rspec, int format = 0) { aoqi@0: _insts.relocate(at, rspec, format); aoqi@0: } aoqi@0: void relocate(address at, relocInfo::relocType rtype, int format = 0) { aoqi@0: _insts.relocate(at, rtype, format); aoqi@0: } aoqi@0: aoqi@0: // Management of overflow storage for binding of Labels. aoqi@0: GrowableArray* create_patch_overflow(); aoqi@0: aoqi@0: // NMethod generation aoqi@0: void copy_code_and_locs_to(CodeBlob* blob) { aoqi@0: assert(blob != NULL, "sane"); aoqi@0: copy_relocations_to(blob); aoqi@0: copy_code_to(blob); aoqi@0: } aoqi@0: void copy_values_to(nmethod* nm) { aoqi@0: if (!oop_recorder()->is_unused()) { aoqi@0: oop_recorder()->copy_values_to(nm); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Transform an address from the code in this code buffer to a specified code buffer aoqi@0: address transform_address(const CodeBuffer &cb, address addr) const; aoqi@0: aoqi@0: void block_comment(intptr_t offset, const char * comment) PRODUCT_RETURN; aoqi@0: const char* code_string(const char* str) PRODUCT_RETURN_(return NULL;); aoqi@0: aoqi@0: // Log a little info about section usage in the CodeBuffer aoqi@0: void log_section_sizes(const char* name); aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: public: aoqi@0: // Printing / Decoding aoqi@0: // decodes from decode_begin() to code_end() and sets decode_begin to end aoqi@0: void decode(); aoqi@0: void decode_all(); // decodes all the code aoqi@0: void skip_decode(); // sets decode_begin to code_end(); aoqi@0: void print(); aoqi@0: #endif aoqi@0: aoqi@0: aoqi@0: // The following header contains architecture-specific implementations aoqi@0: #ifdef TARGET_ARCH_x86 aoqi@0: # include "codeBuffer_x86.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_sparc aoqi@0: # include "codeBuffer_sparc.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_zero aoqi@0: # include "codeBuffer_zero.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_arm aoqi@0: # include "codeBuffer_arm.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_ppc aoqi@0: # include "codeBuffer_ppc.hpp" aoqi@0: #endif aoqi@0: aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: inline void CodeSection::freeze() { aoqi@0: _outer->freeze_section(this); aoqi@0: } aoqi@0: aoqi@0: inline bool CodeSection::maybe_expand_to_ensure_remaining(csize_t amount) { aoqi@0: if (remaining() < amount) { _outer->expand(this, amount); return true; } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: #endif // SHARE_VM_ASM_CODEBUFFER_HPP