Path: blob/master/src/hotspot/share/asm/codeBuffer.hpp
40951 views
/*1* Copyright (c) 1997, 2021, 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_ASM_CODEBUFFER_HPP25#define SHARE_ASM_CODEBUFFER_HPP2627#include "code/oopRecorder.hpp"28#include "code/relocInfo.hpp"29#include "utilities/align.hpp"30#include "utilities/debug.hpp"31#include "utilities/macros.hpp"3233class CodeStrings;34class PhaseCFG;35class Compile;36class BufferBlob;37class CodeBuffer;38class Label;3940class CodeOffsets: public StackObj {41public:42enum Entries { Entry,43Verified_Entry,44Frame_Complete, // Offset in the code where the frame setup is (for forte stackwalks) is complete45OSR_Entry,46Exceptions, // Offset where exception handler lives47Deopt, // Offset where deopt handler lives48DeoptMH, // Offset where MethodHandle deopt handler lives49UnwindHandler, // Offset to default unwind handler50max_Entries };5152// special value to note codeBlobs where profile (forte) stack walking is53// always dangerous and suspect.5455enum { frame_never_safe = -1 };5657private:58int _values[max_Entries];5960public:61CodeOffsets() {62_values[Entry ] = 0;63_values[Verified_Entry] = 0;64_values[Frame_Complete] = frame_never_safe;65_values[OSR_Entry ] = 0;66_values[Exceptions ] = -1;67_values[Deopt ] = -1;68_values[DeoptMH ] = -1;69_values[UnwindHandler ] = -1;70}7172int value(Entries e) { return _values[e]; }73void set_value(Entries e, int val) { _values[e] = val; }74};7576// This class represents a stream of code and associated relocations.77// There are a few in each CodeBuffer.78// They are filled concurrently, and concatenated at the end.79class CodeSection {80friend class CodeBuffer;81public:82typedef int csize_t; // code size type; would be size_t except for history8384private:85address _start; // first byte of contents (instructions)86address _mark; // user mark, usually an instruction beginning87address _end; // current end address88address _limit; // last possible (allocated) end address89relocInfo* _locs_start; // first byte of relocation information90relocInfo* _locs_end; // first byte after relocation information91relocInfo* _locs_limit; // first byte after relocation information buf92address _locs_point; // last relocated position (grows upward)93bool _locs_own; // did I allocate the locs myself?94bool _scratch_emit; // Buffer is used for scratch emit, don't relocate.95char _index; // my section number (SECT_INST, etc.)96CodeBuffer* _outer; // enclosing CodeBuffer9798// (Note: _locs_point used to be called _last_reloc_offset.)99100CodeSection() {101_start = NULL;102_mark = NULL;103_end = NULL;104_limit = NULL;105_locs_start = NULL;106_locs_end = NULL;107_locs_limit = NULL;108_locs_point = NULL;109_locs_own = false;110_scratch_emit = false;111debug_only(_index = (char)-1);112debug_only(_outer = (CodeBuffer*)badAddress);113}114115void initialize_outer(CodeBuffer* outer, int index) {116_outer = outer;117_index = index;118}119120void initialize(address start, csize_t size = 0) {121assert(_start == NULL, "only one init step, please");122_start = start;123_mark = NULL;124_end = start;125126_limit = start + size;127_locs_point = start;128}129130void initialize_locs(int locs_capacity);131void expand_locs(int new_capacity);132void initialize_locs_from(const CodeSection* source_cs);133134// helper for CodeBuffer::expand()135void take_over_code_from(CodeSection* cs) {136_start = cs->_start;137_mark = cs->_mark;138_end = cs->_end;139_limit = cs->_limit;140_locs_point = cs->_locs_point;141}142143public:144address start() const { return _start; }145address mark() const { return _mark; }146address end() const { return _end; }147address limit() const { return _limit; }148csize_t size() const { return (csize_t)(_end - _start); }149csize_t mark_off() const { assert(_mark != NULL, "not an offset");150return (csize_t)(_mark - _start); }151csize_t capacity() const { return (csize_t)(_limit - _start); }152csize_t remaining() const { return (csize_t)(_limit - _end); }153154relocInfo* locs_start() const { return _locs_start; }155relocInfo* locs_end() const { return _locs_end; }156int locs_count() const { return (int)(_locs_end - _locs_start); }157relocInfo* locs_limit() const { return _locs_limit; }158address locs_point() const { return _locs_point; }159csize_t locs_point_off() const{ return (csize_t)(_locs_point - _start); }160csize_t locs_capacity() const { return (csize_t)(_locs_limit - _locs_start); }161162int index() const { return _index; }163bool is_allocated() const { return _start != NULL; }164bool is_empty() const { return _start == _end; }165bool has_locs() const { return _locs_end != NULL; }166167// Mark scratch buffer.168void set_scratch_emit() { _scratch_emit = true; }169bool scratch_emit() { return _scratch_emit; }170171CodeBuffer* outer() const { return _outer; }172173// is a given address in this section? (2nd version is end-inclusive)174bool contains(address pc) const { return pc >= _start && pc < _end; }175bool contains2(address pc) const { return pc >= _start && pc <= _end; }176bool allocates(address pc) const { return pc >= _start && pc < _limit; }177bool allocates2(address pc) const { return pc >= _start && pc <= _limit; }178179// checks if two CodeSections are disjoint180//181// limit is an exclusive address and can be the start of another182// section.183bool disjoint(CodeSection* cs) const { return cs->_limit <= _start || cs->_start >= _limit; }184185void set_end(address pc) { assert(allocates2(pc), "not in CodeBuffer memory: " INTPTR_FORMAT " <= " INTPTR_FORMAT " <= " INTPTR_FORMAT, p2i(_start), p2i(pc), p2i(_limit)); _end = pc; }186void set_mark(address pc) { assert(contains2(pc), "not in codeBuffer");187_mark = pc; }188void set_mark() { _mark = _end; }189void clear_mark() { _mark = NULL; }190191void set_locs_end(relocInfo* p) {192assert(p <= locs_limit(), "locs data fits in allocated buffer");193_locs_end = p;194}195void set_locs_point(address pc) {196assert(pc >= locs_point(), "relocation addr may not decrease");197assert(allocates2(pc), "relocation addr must be in this section");198_locs_point = pc;199}200201// Code emission202void emit_int8(int8_t x1) {203address curr = end();204*((int8_t*) curr++) = x1;205set_end(curr);206}207208void emit_int16(int16_t x) { *((int16_t*) end()) = x; set_end(end() + sizeof(int16_t)); }209void emit_int16(int8_t x1, int8_t x2) {210address curr = end();211*((int8_t*) curr++) = x1;212*((int8_t*) curr++) = x2;213set_end(curr);214}215216void emit_int24(int8_t x1, int8_t x2, int8_t x3) {217address curr = end();218*((int8_t*) curr++) = x1;219*((int8_t*) curr++) = x2;220*((int8_t*) curr++) = x3;221set_end(curr);222}223224void emit_int32(int32_t x) { *((int32_t*) end()) = x; set_end(end() + sizeof(int32_t)); }225void emit_int32(int8_t x1, int8_t x2, int8_t x3, int8_t x4) {226address curr = end();227*((int8_t*) curr++) = x1;228*((int8_t*) curr++) = x2;229*((int8_t*) curr++) = x3;230*((int8_t*) curr++) = x4;231set_end(curr);232}233234void emit_int64( int64_t x) { *((int64_t*) end()) = x; set_end(end() + sizeof(int64_t)); }235236void emit_float( jfloat x) { *((jfloat*) end()) = x; set_end(end() + sizeof(jfloat)); }237void emit_double(jdouble x) { *((jdouble*) end()) = x; set_end(end() + sizeof(jdouble)); }238void emit_address(address x) { *((address*) end()) = x; set_end(end() + sizeof(address)); }239240// Share a scratch buffer for relocinfo. (Hacky; saves a resource allocation.)241void initialize_shared_locs(relocInfo* buf, int length);242243// Manage labels and their addresses.244address target(Label& L, address branch_pc);245246// Emit a relocation.247void relocate(address at, RelocationHolder const& rspec, int format = 0);248void relocate(address at, relocInfo::relocType rtype, int format = 0, jint method_index = 0);249250// alignment requirement for starting offset251// Requirements are that the instruction area and the252// stubs area must start on CodeEntryAlignment, and253// the ctable on sizeof(jdouble)254int alignment() const { return MAX2((int)sizeof(jdouble), (int)CodeEntryAlignment); }255256// Slop between sections, used only when allocating temporary BufferBlob buffers.257static csize_t end_slop() { return MAX2((int)sizeof(jdouble), (int)CodeEntryAlignment); }258259csize_t align_at_start(csize_t off) const { return (csize_t) align_up(off, alignment()); }260261// Ensure there's enough space left in the current section.262// Return true if there was an expansion.263bool maybe_expand_to_ensure_remaining(csize_t amount);264265#ifndef PRODUCT266void decode();267void print(const char* name);268#endif //PRODUCT269};270271class CodeString;272class CodeStrings {273private:274#ifndef PRODUCT275CodeString* _strings;276CodeString* _strings_last;277#ifdef ASSERT278// Becomes true after copy-out, forbids further use.279bool _defunct; // Zero bit pattern is "valid", see memset call in decode_env::decode_env280#endif281static const char* _prefix; // defaults to " ;; "282283CodeString* find(intptr_t offset) const;284CodeString* find_last(intptr_t offset) const;285286void set_null_and_invalidate() {287_strings = NULL;288_strings_last = NULL;289#ifdef ASSERT290_defunct = true;291#endif292}293#endif294295public:296CodeStrings() {297#ifndef PRODUCT298_strings = NULL;299_strings_last = NULL;300#ifdef ASSERT301_defunct = false;302#endif303#endif304}305306#ifndef PRODUCT307bool is_null() {308#ifdef ASSERT309return _strings == NULL;310#else311return true;312#endif313}314315const char* add_string(const char * string);316317void add_comment(intptr_t offset, const char * comment);318void print_block_comment(outputStream* stream, intptr_t offset) const;319int count() const;320// COPY strings from other to this; leave other valid.321void copy(CodeStrings& other);322// FREE strings; invalidate this.323void free();324325// Guarantee that _strings are used at most once; assign and free invalidate a buffer.326inline void check_valid() const {327assert(!_defunct, "Use of invalid CodeStrings");328}329330static void set_prefix(const char *prefix) {331_prefix = prefix;332}333#endif // !PRODUCT334};335336// A CodeBuffer describes a memory space into which assembly337// code is generated. This memory space usually occupies the338// interior of a single BufferBlob, but in some cases it may be339// an arbitrary span of memory, even outside the code cache.340//341// A code buffer comes in two variants:342//343// (1) A CodeBuffer referring to an already allocated piece of memory:344// This is used to direct 'static' code generation (e.g. for interpreter345// or stubroutine generation, etc.). This code comes with NO relocation346// information.347//348// (2) A CodeBuffer referring to a piece of memory allocated when the349// CodeBuffer is allocated. This is used for nmethod generation.350//351// The memory can be divided up into several parts called sections.352// Each section independently accumulates code (or data) an relocations.353// Sections can grow (at the expense of a reallocation of the BufferBlob354// and recopying of all active sections). When the buffered code is finally355// written to an nmethod (or other CodeBlob), the contents (code, data,356// and relocations) of the sections are padded to an alignment and concatenated.357// Instructions and data in one section can contain relocatable references to358// addresses in a sibling section.359360class CodeBuffer: public StackObj {361friend class CodeSection;362friend class StubCodeGenerator;363364private:365// CodeBuffers must be allocated on the stack except for a single366// special case during expansion which is handled internally. This367// is done to guarantee proper cleanup of resources.368void* operator new(size_t size) throw() { return ResourceObj::operator new(size); }369void operator delete(void* p) { ShouldNotCallThis(); }370371public:372typedef int csize_t; // code size type; would be size_t except for history373enum {374// Here is the list of all possible sections. The order reflects375// the final layout.376SECT_FIRST = 0,377SECT_CONSTS = SECT_FIRST, // Non-instruction data: Floats, jump tables, etc.378SECT_INSTS, // Executable instructions.379SECT_STUBS, // Outbound trampolines for supporting call sites.380SECT_LIMIT, SECT_NONE = -1381};382383private:384enum {385sect_bits = 2, // assert (SECT_LIMIT <= (1<<sect_bits))386sect_mask = (1<<sect_bits)-1387};388389const char* _name;390391CodeSection _consts; // constants, jump tables392CodeSection _insts; // instructions (the main section)393CodeSection _stubs; // stubs (call site support), deopt, exception handling394395CodeBuffer* _before_expand; // dead buffer, from before the last expansion396397BufferBlob* _blob; // optional buffer in CodeCache for generated code398address _total_start; // first address of combined memory buffer399csize_t _total_size; // size in bytes of combined memory buffer400401OopRecorder* _oop_recorder;402403OopRecorder _default_oop_recorder; // override with initialize_oop_recorder404Arena* _overflow_arena;405406address _last_insn; // used to merge consecutive memory barriers, loads or stores.407408#ifndef PRODUCT409CodeStrings _code_strings;410bool _collect_comments; // Indicate if we need to collect block comments at all.411address _decode_begin; // start address for decode412address decode_begin();413#endif414415void initialize_misc(const char * name) {416// all pointers other than code_start/end and those inside the sections417assert(name != NULL, "must have a name");418_name = name;419_before_expand = NULL;420_blob = NULL;421_oop_recorder = NULL;422_overflow_arena = NULL;423_last_insn = NULL;424425#ifndef PRODUCT426_decode_begin = NULL;427_code_strings = CodeStrings();428// Collect block comments, but restrict collection to cases where a disassembly is output.429_collect_comments = ( PrintAssembly430|| PrintStubCode431|| PrintMethodHandleStubs432|| PrintInterpreter433|| PrintSignatureHandlers434|| UnlockDiagnosticVMOptions435);436#endif437}438439void initialize(address code_start, csize_t code_size) {440_consts.initialize_outer(this, SECT_CONSTS);441_insts.initialize_outer(this, SECT_INSTS);442_stubs.initialize_outer(this, SECT_STUBS);443_total_start = code_start;444_total_size = code_size;445// Initialize the main section:446_insts.initialize(code_start, code_size);447assert(!_stubs.is_allocated(), "no garbage here");448assert(!_consts.is_allocated(), "no garbage here");449_oop_recorder = &_default_oop_recorder;450}451452void initialize_section_size(CodeSection* cs, csize_t size);453454// helper for CodeBuffer::expand()455void take_over_code_from(CodeBuffer* cs);456457// ensure sections are disjoint, ordered, and contained in the blob458void verify_section_allocation();459460// copies combined relocations to the blob, returns bytes copied461// (if target is null, it is a dry run only, just for sizing)462csize_t copy_relocations_to(CodeBlob* blob) const;463464// copies combined code to the blob (assumes relocs are already in there)465void copy_code_to(CodeBlob* blob);466467// moves code sections to new buffer (assumes relocs are already in there)468void relocate_code_to(CodeBuffer* cb) const;469470// set up a model of the final layout of my contents471void compute_final_layout(CodeBuffer* dest) const;472473// Expand the given section so at least 'amount' is remaining.474// Creates a new, larger BufferBlob, and rewrites the code & relocs.475void expand(CodeSection* which_cs, csize_t amount);476477// Helper for expand.478csize_t figure_expanded_capacities(CodeSection* which_cs, csize_t amount, csize_t* new_capacity);479480public:481// (1) code buffer referring to pre-allocated instruction memory482CodeBuffer(address code_start, csize_t code_size) {483assert(code_start != NULL, "sanity");484initialize_misc("static buffer");485initialize(code_start, code_size);486debug_only(verify_section_allocation();)487}488489// (2) CodeBuffer referring to pre-allocated CodeBlob.490CodeBuffer(CodeBlob* blob);491492// (3) code buffer allocating codeBlob memory for code & relocation493// info but with lazy initialization. The name must be something494// informative.495CodeBuffer(const char* name) {496initialize_misc(name);497}498499// (4) code buffer allocating codeBlob memory for code & relocation500// info. The name must be something informative and code_size must501// include both code and stubs sizes.502CodeBuffer(const char* name, csize_t code_size, csize_t locs_size) {503initialize_misc(name);504initialize(code_size, locs_size);505}506507~CodeBuffer();508509// Initialize a CodeBuffer constructed using constructor 3. Using510// constructor 4 is equivalent to calling constructor 3 and then511// calling this method. It's been factored out for convenience of512// construction.513void initialize(csize_t code_size, csize_t locs_size);514515CodeSection* consts() { return &_consts; }516CodeSection* insts() { return &_insts; }517CodeSection* stubs() { return &_stubs; }518519const CodeSection* insts() const { return &_insts; }520521// present sections in order; return NULL at end; consts is #0, etc.522CodeSection* code_section(int n) {523// This makes the slightly questionable but portable assumption524// that the various members (_consts, _insts, _stubs, etc.) are525// adjacent in the layout of CodeBuffer.526CodeSection* cs = &_consts + n;527assert(cs->index() == n || !cs->is_allocated(), "sanity");528return cs;529}530const CodeSection* code_section(int n) const { // yucky const stuff531return ((CodeBuffer*)this)->code_section(n);532}533static const char* code_section_name(int n);534int section_index_of(address addr) const;535bool contains(address addr) const {536// handy for debugging537return section_index_of(addr) > SECT_NONE;538}539540// A stable mapping between 'locators' (small ints) and addresses.541static int locator_pos(int locator) { return locator >> sect_bits; }542static int locator_sect(int locator) { return locator & sect_mask; }543static int locator(int pos, int sect) { return (pos << sect_bits) | sect; }544int locator(address addr) const;545address locator_address(int locator) const {546if (locator < 0) return NULL;547address start = code_section(locator_sect(locator))->start();548return start + locator_pos(locator);549}550551// Heuristic for pre-packing the taken/not-taken bit of a predicted branch.552bool is_backward_branch(Label& L);553554// Properties555const char* name() const { return _name; }556void set_name(const char* name) { _name = name; }557CodeBuffer* before_expand() const { return _before_expand; }558BufferBlob* blob() const { return _blob; }559void set_blob(BufferBlob* blob);560void free_blob(); // Free the blob, if we own one.561562// Properties relative to the insts section:563address insts_begin() const { return _insts.start(); }564address insts_end() const { return _insts.end(); }565void set_insts_end(address end) { _insts.set_end(end); }566address insts_mark() const { return _insts.mark(); }567void set_insts_mark() { _insts.set_mark(); }568569// is there anything in the buffer other than the current section?570bool is_pure() const { return insts_size() == total_content_size(); }571572// size in bytes of output so far in the insts sections573csize_t insts_size() const { return _insts.size(); }574575// same as insts_size(), except that it asserts there is no non-code here576csize_t pure_insts_size() const { assert(is_pure(), "no non-code");577return insts_size(); }578// capacity in bytes of the insts sections579csize_t insts_capacity() const { return _insts.capacity(); }580581// number of bytes remaining in the insts section582csize_t insts_remaining() const { return _insts.remaining(); }583584// is a given address in the insts section? (2nd version is end-inclusive)585bool insts_contains(address pc) const { return _insts.contains(pc); }586bool insts_contains2(address pc) const { return _insts.contains2(pc); }587588// Record any extra oops required to keep embedded metadata alive589void finalize_oop_references(const methodHandle& method);590591// Allocated size in all sections, when aligned and concatenated592// (this is the eventual state of the content in its final593// CodeBlob).594csize_t total_content_size() const;595596// Combined offset (relative to start of first section) of given597// section, as eventually found in the final CodeBlob.598csize_t total_offset_of(const CodeSection* cs) const;599600// allocated size of all relocation data, including index, rounded up601csize_t total_relocation_size() const;602603csize_t copy_relocations_to(address buf, csize_t buf_limit, bool only_inst) const;604605// allocated size of any and all recorded oops606csize_t total_oop_size() const {607OopRecorder* recorder = oop_recorder();608return (recorder == NULL)? 0: recorder->oop_size();609}610611// allocated size of any and all recorded metadata612csize_t total_metadata_size() const {613OopRecorder* recorder = oop_recorder();614return (recorder == NULL)? 0: recorder->metadata_size();615}616617// Configuration functions, called immediately after the CB is constructed.618// The section sizes are subtracted from the original insts section.619// Note: Call them in reverse section order, because each steals from insts.620void initialize_consts_size(csize_t size) { initialize_section_size(&_consts, size); }621void initialize_stubs_size(csize_t size) { initialize_section_size(&_stubs, size); }622// Override default oop recorder.623void initialize_oop_recorder(OopRecorder* r);624625OopRecorder* oop_recorder() const { return _oop_recorder; }626627address last_insn() const { return _last_insn; }628void set_last_insn(address a) { _last_insn = a; }629void clear_last_insn() { set_last_insn(NULL); }630631#ifndef PRODUCT632CodeStrings& strings() { return _code_strings; }633634void free_strings() {635if (!_code_strings.is_null()) {636_code_strings.free(); // sets _strings Null as a side-effect.637}638}639#endif640641// Code generation642void relocate(address at, RelocationHolder const& rspec, int format = 0) {643_insts.relocate(at, rspec, format);644}645void relocate(address at, relocInfo::relocType rtype, int format = 0) {646_insts.relocate(at, rtype, format);647}648649// Management of overflow storage for binding of Labels.650GrowableArray<int>* create_patch_overflow();651652// NMethod generation653void copy_code_and_locs_to(CodeBlob* blob) {654assert(blob != NULL, "sane");655copy_relocations_to(blob);656copy_code_to(blob);657}658void copy_values_to(nmethod* nm) {659if (!oop_recorder()->is_unused()) {660oop_recorder()->copy_values_to(nm);661}662}663664void block_comment(intptr_t offset, const char * comment) PRODUCT_RETURN;665const char* code_string(const char* str) PRODUCT_RETURN_(return NULL;);666667// Log a little info about section usage in the CodeBuffer668void log_section_sizes(const char* name);669670#ifndef PRODUCT671public:672// Printing / Decoding673// decodes from decode_begin() to code_end() and sets decode_begin to end674void decode();675void print();676#endif677// Directly disassemble code buffer.678void decode(address start, address end);679680// The following header contains architecture-specific implementations681#include CPU_HEADER(codeBuffer)682683};684685inline bool CodeSection::maybe_expand_to_ensure_remaining(csize_t amount) {686if (remaining() < amount) { _outer->expand(this, amount); return true; }687return false;688}689690#endif // SHARE_ASM_CODEBUFFER_HPP691692693