/* * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *
*/
class PhaseCFG; class Compile; class BufferBlob; class CodeBuffer; class Label; class ciMethod; class SharedStubToInterpRequest;
class CodeOffsets: public StackObj { public: enum Entries { Entry,
Verified_Entry,
Frame_Complete, // Offset in the code where the frame setup is (for forte stackwalks) is complete
OSR_Entry,
Exceptions, // Offset where exception handler lives
Deopt, // Offset where deopt handler lives
DeoptMH, // Offset where MethodHandle deopt handler lives
UnwindHandler, // Offset to default unwind handler
max_Entries };
// special value to note codeBlobs where profile (forte) stack walking is // always dangerous and suspect.
int value(Entries e) { return _values[e]; } void set_value(Entries e, int val) { _values[e] = val; }
};
// This class represents a stream of code and associated relocations. // There are a few in each CodeBuffer. // They are filled concurrently, and concatenated at the end. class CodeSection { friendclass CodeBuffer; public: typedefint csize_t; // code size type; would be size_t except for history
private:
address _start; // first byte of contents (instructions)
address _mark; // user mark, usually an instruction beginning
address _end; // current end address
address _limit; // last possible (allocated) end address
relocInfo* _locs_start; // first byte of relocation information
relocInfo* _locs_end; // first byte after relocation information
relocInfo* _locs_limit; // first byte after relocation information buf
address _locs_point; // last relocated position (grows upward) bool _locs_own; // did I allocate the locs myself? bool _scratch_emit; // Buffer is used for scratch emit, don't relocate. char _index; // my section number (SECT_INST, etc.)
CodeBuffer* _outer; // enclosing CodeBuffer
// (Note: _locs_point used to be called _last_reloc_offset.)
// is a given address in this section? (2nd version is end-inclusive) bool contains(address pc) const { return pc >= _start && pc < _end; } bool contains2(address pc) const { return pc >= _start && pc <= _end; } bool allocates(address pc) const { return pc >= _start && pc < _limit; } bool allocates2(address pc) const { return pc >= _start && pc <= _limit; }
// checks if two CodeSections are disjoint // // limit is an exclusive address and can be the start of another // section. bool disjoint(CodeSection* cs) const { return cs->_limit <= _start || cs->_start >= _limit; }
// Share a scratch buffer for relocinfo. (Hacky; saves a resource allocation.) void initialize_shared_locs(relocInfo* buf, int length);
// Manage labels and their addresses.
address target(Label& L, address branch_pc);
// Emit a relocation. void relocate(address at, RelocationHolder const& rspec, int format = 0); void relocate(address at, relocInfo::relocType rtype, int format = 0, jint method_index = 0);
int alignment() const;
// Slop between sections, used only when allocating temporary BufferBlob buffers. static csize_t end_slop() { return MAX2((int)sizeof(jdouble), (int)CodeEntryAlignment); }
// Ensure there's enough space left in the current section. // Return true if there was an expansion. bool maybe_expand_to_ensure_remaining(csize_t amount);
class AsmRemarkCollection; class DbgStringCollection;
// The assumption made here is that most code remarks (or comments) added to // the generated assembly code are unique, i.e. there is very little gain in // trying to share the strings between the different offsets tracked in a // buffer (or blob).
class AsmRemarks { public:
AsmRemarks();
~AsmRemarks();
// A CodeBuffer describes a memory space into which assembly // code is generated. This memory space usually occupies the // interior of a single BufferBlob, but in some cases it may be // an arbitrary span of memory, even outside the code cache. // // A code buffer comes in two variants: // // (1) A CodeBuffer referring to an already allocated piece of memory: // This is used to direct 'static' code generation (e.g. for interpreter // or stubroutine generation, etc.). This code comes with NO relocation // information. // // (2) A CodeBuffer referring to a piece of memory allocated when the // CodeBuffer is allocated. This is used for nmethod generation. // // The memory can be divided up into several parts called sections. // Each section independently accumulates code (or data) an relocations. // Sections can grow (at the expense of a reallocation of the BufferBlob // and recopying of all active sections). When the buffered code is finally // written to an nmethod (or other CodeBlob), the contents (code, data, // and relocations) of the sections are padded to an alignment and concatenated. // Instructions and data in one section can contain relocatable references to // addresses in a sibling section.
class CodeBuffer: public StackObj DEBUG_ONLY(COMMA private Scrubber) { friendclass CodeSection; friendclass StubCodeGenerator;
private: // CodeBuffers must be allocated on the stack except for a single // special case during expansion which is handled internally. This // is done to guarantee proper cleanup of resources. void* operatornew(size_t size) throw() { return resource_allocate_bytes(size); } voidoperatordelete(void* p) { ShouldNotCallThis(); }
public: typedefint csize_t; // code size type; would be size_t except for history enum { // Here is the list of all possible sections. The order reflects // the final layout.
SECT_FIRST = 0,
SECT_CONSTS = SECT_FIRST, // Non-instruction data: Floats, jump tables, etc.
SECT_INSTS, // Executable instructions.
SECT_STUBS, // Outbound trampolines for supporting call sites.
SECT_LIMIT, SECT_NONE = -1
};
CodeSection _consts; // constants, jump tables
CodeSection _insts; // instructions (the main section)
CodeSection _stubs; // stubs (call site support), deopt, exception handling
CodeBuffer* _before_expand; // dead buffer, from before the last expansion
BufferBlob* _blob; // optional buffer in CodeCache for generated code
address _total_start; // first address of combined memory buffer
csize_t _total_size; // size in bytes of combined memory buffer
OopRecorder* _oop_recorder;
OopRecorder _default_oop_recorder; // override with initialize_oop_recorder
Arena* _overflow_arena;
address _last_insn; // used to merge consecutive memory barriers, loads or stores.
SharedStubToInterpRequests* _shared_stub_to_interp_requests; // used to collect requests for shared iterpreter stubs
SharedTrampolineRequests* _shared_trampoline_requests; // used to collect requests for shared trampolines bool _finalize_stubs; // Indicate if we need to finalize stubs to make CodeBuffer final.
int _const_section_alignment;
#ifndef PRODUCT
AsmRemarks _asm_remarks;
DbgStrings _dbg_strings; bool _collect_comments; // Indicate if we need to collect block comments at all.
address _decode_begin; // start address for decode
address decode_begin(); #endif
void initialize_misc(constchar * 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;
_overflow_arena = NULL;
_last_insn = NULL;
_finalize_stubs = false;
_shared_stub_to_interp_requests = NULL;
_shared_trampoline_requests = NULL;
// Default is to align on 8 bytes. A compiler can change this // if larger alignment (e.g., 32-byte vector masks) is required.
_const_section_alignment = (int) sizeof(jdouble);
#ifndef PRODUCT
_decode_begin = NULL; // Collect block comments, but restrict collection to cases where a disassembly is output.
_collect_comments = ( PrintAssembly
|| PrintStubCode
|| PrintMethodHandleStubs
|| PrintInterpreter
|| PrintSignatureHandlers
|| UnlockDiagnosticVMOptions
); #endif
}
void initialize(address code_start, csize_t code_size) {
_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;
}
// 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);
// (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(constchar* name)
DEBUG_ONLY(: Scrubber(this, sizeof(*this)))
{
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(constchar* name, csize_t code_size, csize_t locs_size)
DEBUG_ONLY(: Scrubber(this, sizeof(*this)))
{
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);
// 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);
} staticconstchar* 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;
}
// 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); }
// Record any extra oops required to keep embedded metadata alive void finalize_oop_references(const methodHandle& method);
// 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(const 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();
}
// allocated size of any and all recorded metadata
csize_t total_metadata_size() const {
OopRecorder* recorder = oop_recorder(); return (recorder == NULL)? 0: recorder->metadata_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);
#ifndef PRODUCT public: // Printing / Decoding // decodes from decode_begin() to code_end() and sets decode_begin to end void decode(); void print(); #endif // Directly disassemble code buffer. void decode(address start, address end);
// The following header contains architecture-specific implementations #include CPU_HEADER(codeBuffer)
};
// A Java method can have calls of Java methods which can be statically bound. // Calls of Java methods need stubs to the interpreter. Calls sharing the same Java method // can share a stub to the interpreter. // A SharedStubToInterpRequest is a request for a shared stub to the interpreter. class SharedStubToInterpRequest : public ResourceObj { private:
ciMethod* _shared_method;
CodeBuffer::csize_t _call_offset; // The offset of the call in CodeBuffer
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.