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